Skip to content

Commit

Permalink
Add login hooks and store userId in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
META-DREAMER committed Jan 17, 2017
1 parent 7da925e commit b250f52
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 28 deletions.
16 changes: 16 additions & 0 deletions client/src/Events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default {
_cbs: [],
notify (eventName) {
this._cbs.map(cb => {
if (cb.eventName === eventName && typeof cb.callback === 'function') {
cb.callback()
}
})
},
on (eventName, cb) {
this._cbs.push({
eventName: eventName,
callback: cb
})
}
}
5 changes: 4 additions & 1 deletion client/src/createUser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import hashPassword from './hashPassword'
import gql from 'graphql-tag'
import {handleLoginCallback, getClient} from './store'
import {handleLoginCallback, getClient, startLoggingIn, endLoggingIn} from './store'

export default async function ({username, email, password, profile}) {
startLoggingIn()
let result
try {
result = await getClient().mutate({
Expand All @@ -24,6 +25,8 @@ export default async function ({username, email, password, profile}) {
})
} catch (err) {
return handleLoginCallback(err)
} finally {
endLoggingIn()
}

return handleLoginCallback(null, result.data.createUser)
Expand Down
15 changes: 12 additions & 3 deletions client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import userId from './userId'
import {
initWithClient,
setTokenStore,
onLogin,
onLoginFailure,
onLogout,
loggingIn,
TOKEN_EXPIRES_KEY,
TOKEN_KEY,
USER_ID_KEY,
onTokenChange
} from './store'

export {
Expand All @@ -33,10 +36,13 @@ export {
loginWithFacebook,
loginWithGoogle,
loginWithLinkedIn,
onTokenChange,
userId,
initWithClient,
setTokenStore,
onLogin,
onLoginFailure,
onLogout,
loggingIn,
TOKEN_EXPIRES_KEY,
TOKEN_KEY,
USER_ID_KEY,
Expand All @@ -54,10 +60,13 @@ export default {
loginWithFacebook,
loginWithGoogle,
loginWithLinkedIn,
onTokenChange,
userId,
initWithClient,
setTokenStore,
onLogin,
onLoginFailure,
onLogout,
loggingIn,
TOKEN_EXPIRES_KEY,
TOKEN_KEY,
USER_ID_KEY,
Expand Down
5 changes: 4 additions & 1 deletion client/src/loginWithPassword.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import hashPassword from './hashPassword'
import gql from 'graphql-tag'
import {handleLoginCallback, getClient} from './store'
import {handleLoginCallback, getClient, startLoggingIn, endLoggingIn} from './store'

export default async function ({username, email, password}) {
startLoggingIn()
let result
try {
result = await getClient().mutate({
Expand All @@ -23,6 +24,8 @@ export default async function ({username, email, password}) {
})
} catch (err) {
return handleLoginCallback(err)
} finally {
endLoggingIn()
}

return handleLoginCallback(null, result.data.loginWithPassword)
Expand Down
4 changes: 2 additions & 2 deletions client/src/logout.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import gql from 'graphql-tag'
import {getLoginToken, resetStore, getClient} from './store'
import {getLoginToken, handleLogout, getClient} from './store'

export default async function () {
const result = await getClient().mutate({
Expand All @@ -15,6 +15,6 @@ export default async function () {
}
})

await resetStore()
await handleLogout()
return result.data.logout.success
}
5 changes: 4 additions & 1 deletion client/src/oauth/loginWithFacebook.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import gql from 'graphql-tag'
import {handleLoginCallback, getClient} from '../store'
import {handleLoginCallback, getClient, startLoggingIn, endLoggingIn} from '../store'

/**
* Pass the accessToken
* It's recommended to use https://github.com/keppelen/react-facebook-login
*/

export default async function ({accessToken}) {
startLoggingIn()
let result
try {
result = await getClient().mutate({
Expand All @@ -25,6 +26,8 @@ export default async function ({accessToken}) {
})
} catch (err) {
return handleLoginCallback(err)
} finally {
endLoggingIn()
}

return handleLoginCallback(null, result.data.loginWithFacebook)
Expand Down
5 changes: 4 additions & 1 deletion client/src/oauth/loginWithGoogle.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import gql from 'graphql-tag'
import {handleLoginCallback, getClient} from '../store'
import {handleLoginCallback, getClient, startLoggingIn, endLoggingIn} from '../store'

/**
* Pass the accessToken
* It's recommended to use https://github.com/anthonyjgrove/react-google-login
*/

export default async function ({accessToken}) {
startLoggingIn()
let result
try {
result = await getClient().mutate({
Expand All @@ -25,6 +26,8 @@ export default async function ({accessToken}) {
})
} catch (err) {
return handleLoginCallback(err)
} finally {
endLoggingIn()
}

return handleLoginCallback(null, result.data.loginWithGoogle)
Expand Down
5 changes: 4 additions & 1 deletion client/src/oauth/loginWithLinkedIn.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import gql from 'graphql-tag'
import {handleLoginCallback, getClient} from '../store'
import {handleLoginCallback, getClient, startLoggingIn, endLoggingIn} from '../store'

/**
* Pass the accessToken
* It's recommended to use https://github.com/keppelen/react-facebook-login
*/

export default async function ({code, redirectUri}) {
startLoggingIn()
let result
try {
result = await getClient().mutate({
Expand All @@ -23,6 +24,8 @@ export default async function ({code, redirectUri}) {
})
} catch (err) {
return handleLoginCallback(err)
} finally {
endLoggingIn()
}

return handleLoginCallback(null, result.data.loginWithLinkedIn)
Expand Down
56 changes: 38 additions & 18 deletions client/src/store.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import Events from './Events'

const onChangeCallbacks = []
export const USER_ID_KEY = 'Meteor.userId'
export const TOKEN_KEY = 'Meteor.loginToken'
export const TOKEN_EXPIRES_KEY = 'Meteor.loginTokenExpires'

let _tokenSaved
let _userIdSaved
let _isLoggingIn = true
let apollo

let tokenStore = {
Expand All @@ -18,6 +21,11 @@ let tokenStore = {
token: global.localStorage[TOKEN_KEY],
tokenExpires: global.localStorage[TOKEN_EXPIRES_KEY]
}
},
remove: async function () {
global.localStorage.removeItem(USER_ID_KEY)
global.localStorage.removeItem(TOKEN_KEY)
global.localStorage.removeItem(TOKEN_EXPIRES_KEY)
}
}

Expand All @@ -40,39 +48,51 @@ export const handleLoginCallback = async function (err, loginMethodResponse) {
if (!err) { // save user id and token
const {id, token, tokenExpires} = loginMethodResponse
await _storeLoginToken(id, token, new Date(tokenExpires))
_tokenSaved = token
_userIdSaved = id
Events.notify('onLogin')
return id
} else {
resetStore()
Events.notify('onLoginFailure')
handleLogout()
return Promise.reject(err)
}
}

export const handleLogout = async function () {
await tokenStore.remove()
_tokenSaved = null
_userIdSaved = null
Events.notify('onLogout')
}

export const _storeLoginToken = async function (userId, token, tokenExpires) {
await tokenStore.set({userId, token, tokenExpires})
await tokenDidChange()
}

export const getLoginToken = async function () {
const {token} = await tokenStore.get()
return token
export const loggingIn = function () {
return _isLoggingIn
}

export const getUserId = async function () {
const {userId} = await tokenStore.get()
return userId
export const onLogin = function (cb) {
Events.on('onLogin', cb)
}

const tokenDidChange = async function () {
const newData = await tokenStore.get()
for (const callback of onChangeCallbacks) {
callback(newData)
}
export const onLoginFailure = function (cb) {
Events.on('onLoginFailure', cb)
}

export const onTokenChange = function (callback) {
onChangeCallbacks.push(callback)
export const onLogout = function (cb) {
Events.on('onLogout', cb)
}

export const resetStore = async function () {
await _storeLoginToken('', '', '')
export const startLoggingIn = function () {
_isLoggingIn = true
}

export const endLoggingIn = function () {
_isLoggingIn = false
}

export const getLoginToken = () => _tokenSaved
export const getUserId = () => _userIdSaved

0 comments on commit b250f52

Please sign in to comment.