Skip to content

Commit e094161

Browse files
committed
docs(methods): setup all methods basic reference and examples (#10)
fixes #10
1 parent 7575902 commit e094161

File tree

12 files changed

+162
-4
lines changed

12 files changed

+162
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ It's available for both Node.js and browser environments.
1717
```js
1818
import ecomAuth from '@ecomplus/auth'
1919

20-
ecomAuth.login(localStorage.getItem('username'), localStorage.getItem('password'), localStorage.getItem('store_id'))
20+
ecomAuth.login(localStorage.getItem('username'), localStorage.getItem('password'))
2121

2222
ecomAuth.on('login', self => {
2323
console.log('Login OK!', self)

src/constructor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const EcomAuth = function (storeId, lang) {
7373
}
7474
})
7575

76-
this.login = (userOrEmail, password) => login(ecomAuth, userOrEmail, password)
76+
this.login = (userOrEmail, password, isMd5Hash) => login(ecomAuth, userOrEmail, password, isMd5Hash)
7777

7878
this.createEcomplusSession = () => createEcomplusSession(ecomAuth)
7979

src/methods/check-login.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* @method
3+
* @name EcomAuth#checkLogin
4+
* @description Check if admin is logged and access token is not expired.
5+
*
6+
* @returns {boolean}
7+
*
8+
* @example
9+
10+
if (ecomAuth.checkLogin()) {
11+
console.log('Logged')
12+
}
13+
14+
*/
15+
116
export default ({ session }) => {
217
return Date.now() < new Date(session.expires).getTime() && Boolean(session.access_token)
318
}

src/methods/create-ecomplus-session.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import axios from 'axios'
22

3+
/**
4+
* @method
5+
* @name EcomAuth#createEcomplusSession
6+
* @description Try set E-Com Plus cross domain session after login.
7+
*
8+
* @returns {Promise<response|error>}
9+
*
10+
* @example
11+
12+
ecomAuth.createEcomplusSession().then(() => console.log('User session created'))
13+
14+
*/
15+
316
export default ({ session, checkLogin }) => {
417
if (!checkLogin()) {
518
return Promise.reject(new Error('Unauthorized, requires login'))

src/methods/fetch-authentication.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
import fetchAndCache from '../lib/fetch-and-cache'
22

3+
/**
4+
* @method
5+
* @name EcomAuth#fetchAuthentication
6+
* @description Fetch current user authentication object from Store API.
7+
*
8+
* @param {boolean} [mustSkipSession=false] - Skips previously fetched result (refresh cache)
9+
*
10+
* @returns {Promise<data|error>}
11+
*
12+
* @example
13+
14+
ecomAuth.fetchAuthentication().then(authentication => {
15+
console.log(authentication._id)
16+
})
17+
18+
*/
19+
320
export default (self, mustSkipSession) => {
421
const url = `/authentications/${self.session.my_id}.json`
522
return fetchAndCache(self, url, mustSkipSession)

src/methods/fetch-store.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
import fetchAndCache from '../lib/fetch-and-cache'
22

3+
/**
4+
* @method
5+
* @name EcomAuth#fetchStore
6+
* @description Fetch current store object from Store API.
7+
*
8+
* @param {boolean} [mustSkipSession=false] - Skips previously fetched result (refresh cache)
9+
*
10+
* @returns {Promise<data|error>}
11+
*
12+
* @example
13+
14+
ecomAuth.fetchStore().then(store => {
15+
console.log(store._id)
16+
})
17+
18+
*/
19+
320
export default (self, mustSkipSession) => {
421
const url = `/stores/${self.session.store_id}.json`
522
return fetchAndCache(self, url, mustSkipSession, 'store')

src/methods/get-authentication-id.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/**
2+
* @method
3+
* @name EcomAuth#getAuthenticationId
4+
* @description Returns current session authentication ID.
5+
*
6+
* @returns {string|null}
7+
*
8+
* @example
9+
10+
console.log(ecomAuth.getAuthenticationId())
11+
12+
*/
13+
114
export default ({ session }) => {
215
if (session && session.my_id) {
316
return session.my_id

src/methods/get-session.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
/**
2+
* @method
3+
* @name EcomAuth#getSession
4+
* @description Returns current session object (without token).
5+
*
6+
* @returns {object}
7+
*
8+
* @example
9+
10+
const session = ecomAuth.getSession()
11+
console.log(session.my_id, session.store_id)
12+
13+
*/
14+
115
export default ({ session }) => {
216
return {
317
...session,

src/methods/login.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
import { store } from '@ecomplus/client'
22
import * as md5 from 'blueimp-md5'
33

4-
export default (self, userOrEmail, password) => {
4+
/**
5+
* @method
6+
* @name EcomAuth#login
7+
* @description Try to login and authenticate admin with email or username and password.
8+
*
9+
* @param {string} userOrEmail - Admin username or email address
10+
* @param {string} password - Password or MD5 hash
11+
* @param {boolean} [isMd5Hash=false] - If password argument is already the MD5 hash string
12+
*
13+
* @returns {Promise<self|error>}
14+
*
15+
* @example
16+
17+
ecomAuth.login('leo', '1234567890').then(() => {
18+
console.log(ecomAuth.getSession())
19+
})
20+
21+
*/
22+
23+
export default (self, userOrEmail, password, isMd5Hash) => {
524
const { storeId, setSession } = self
625

726
let url = '/_login.json'
@@ -20,7 +39,7 @@ export default (self, userOrEmail, password) => {
2039
data: {
2140
email,
2241
username,
23-
pass_md5_hash: md5(password)
42+
pass_md5_hash: isMd5Hash === true ? password : md5(password)
2443
}
2544
})
2645

src/methods/logout.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import emitter from '../lib/emitter'
22

3+
/**
4+
* @method
5+
* @name EcomAuth#logout
6+
* @description Reset admin session object.
7+
*
8+
* @returns {self}
9+
*
10+
* @example
11+
12+
ecomAuth.logout()
13+
14+
*/
15+
316
export default (self) => {
417
self.session.access_token = ''
518
emitter.emit('logout', self)

0 commit comments

Comments
 (0)