|
| 1 | +import { useReducer, useEffect, useMemo } from 'react' |
| 2 | +import Meteor from '@meteorrn/core' |
| 3 | + |
| 4 | +/** @private */ |
| 5 | +const initialState = { |
| 6 | + isLoading: true, |
| 7 | + isSignout: false, |
| 8 | + userToken: null |
| 9 | +} |
| 10 | + |
| 11 | +/** @private */ |
| 12 | +const reducer = (state, action) => { |
| 13 | + switch (action.type) { |
| 14 | + case 'RESTORE_TOKEN': |
| 15 | + return { |
| 16 | + ...state, |
| 17 | + userToken: action.token, |
| 18 | + isLoading: false |
| 19 | + } |
| 20 | + case 'SIGN_IN': |
| 21 | + return { |
| 22 | + ...state, |
| 23 | + isSignOut: false, |
| 24 | + userToken: action.token |
| 25 | + } |
| 26 | + case 'SIGN_OUT': |
| 27 | + return { |
| 28 | + ...state, |
| 29 | + isSignout: true, |
| 30 | + userToken: null |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/** @private */ |
| 36 | +const Data = Meteor.getData() |
| 37 | + |
| 38 | +/** |
| 39 | + * Provides a state and authentication context for components to decide, whether |
| 40 | + * the user is authenticated and also to run several authentication actions. |
| 41 | + * |
| 42 | + * The returned state contains the following structure: |
| 43 | + * {{ |
| 44 | + * isLoading: boolean, |
| 45 | + * isSignout: boolean, |
| 46 | + * userToken: string|null |
| 47 | + * } |
| 48 | + * }} |
| 49 | + * |
| 50 | + * the authcontext provides the following methods: |
| 51 | + * {{ |
| 52 | + * signIn: function, |
| 53 | + * signOut: function, |
| 54 | + * signUp: function |
| 55 | + * }} |
| 56 | + * |
| 57 | + * @returns {{ |
| 58 | + * state:object, |
| 59 | + * authContext: object |
| 60 | + * }} |
| 61 | + */ |
| 62 | +export const useLogin = () => { |
| 63 | + const [state, dispatch] = useReducer(reducer, initialState, undefined) |
| 64 | + |
| 65 | + // Case 1: restore token already exists |
| 66 | + // MeteorRN loads the token on connection automatically, |
| 67 | + // in case it exists, but we need to "know" that for our auth workflow |
| 68 | + useEffect(() => { |
| 69 | + const handleOnLogin = () => dispatch({ type: 'RESTORE_TOKEN', token: Meteor.getAuthToken() }) |
| 70 | + Data.on('onLogin', handleOnLogin) |
| 71 | + return () => Data.off('onLogin', handleOnLogin) |
| 72 | + }, []) |
| 73 | + |
| 74 | + // the auth can be referenced via useContext in the several |
| 75 | + // screens later on |
| 76 | + const authContext = useMemo(() => ({ |
| 77 | + signIn: ({ email, password, onError }) => { |
| 78 | + Meteor.loginWithPassword(email, password, async (err) => { |
| 79 | + if (err) { |
| 80 | + if (err.message === 'Match failed [400]') { |
| 81 | + err.message = 'Login failed, please check your credentials and retry.' |
| 82 | + } |
| 83 | + return onError(err) |
| 84 | + } |
| 85 | + const token = Meteor.getAuthToken() |
| 86 | + const type = 'SIGN_IN' |
| 87 | + dispatch({ type, token }) |
| 88 | + }) |
| 89 | + }, |
| 90 | + signOut: () => { |
| 91 | + Meteor.logout(err => { |
| 92 | + if (err) { |
| 93 | + // TODO display error, merge into the above workflow |
| 94 | + return console.error(err) |
| 95 | + } |
| 96 | + dispatch({ type: 'SIGN_OUT' }) |
| 97 | + }) |
| 98 | + }, |
| 99 | + signUp: ({ email, password, onError }) => { |
| 100 | + Meteor.call('register', { email, password }, (err, res) => { |
| 101 | + if (err) { |
| 102 | + return onError(err) |
| 103 | + } |
| 104 | + // TODO move the below code and the code from signIn into an own function |
| 105 | + Meteor.loginWithPassword(email, password, async (err) => { |
| 106 | + if (err) { |
| 107 | + if (err.message === 'Match failed [400]') { |
| 108 | + err.message = 'Login failed, please check your credentials and retry.' |
| 109 | + } |
| 110 | + return onError(err) |
| 111 | + } |
| 112 | + const token = Meteor.getAuthToken() |
| 113 | + const type = 'SIGN_IN' |
| 114 | + dispatch({ type, token }) |
| 115 | + }) |
| 116 | + }) |
| 117 | + } |
| 118 | + }), []) |
| 119 | + |
| 120 | + return { state, authContext } |
| 121 | +} |
0 commit comments