11import { useReducer , useEffect , useMemo } from 'react'
22import Meteor from '@meteorrn/core'
33
4- /** @private */
54const initialState = {
65 isLoading : true ,
76 isSignout : false ,
87 userToken : null
98}
109
11- /** @private */
1210const reducer = ( state , action ) => {
1311 switch ( action . type ) {
1412 case 'RESTORE_TOKEN' :
@@ -32,34 +30,18 @@ const reducer = (state, action) => {
3230 }
3331}
3432
35- /** @private */
3633const Data = Meteor . getData ( )
3734
3835/**
3936 * Provides a state and authentication context for components to decide, whether
4037 * the user is authenticated and also to run several authentication actions.
4138 *
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- *
5739 * @returns {{
5840 * state:object,
5941 * authContext: object
6042 * }}
6143 */
62- export const useLogin = ( ) => {
44+ export const useAuth = ( ) => {
6345 const [ state , dispatch ] = useReducer ( reducer , initialState , undefined )
6446
6547 // Case 1: restore token already exists
@@ -71,8 +53,17 @@ export const useLogin = () => {
7153 return ( ) => Data . off ( 'onLogin' , handleOnLogin )
7254 } , [ ] )
7355
74- // the auth can be referenced via useContext in the several
75- // screens later on
56+ /**
57+ * Bridge between the backend endpoints and client.
58+ * Get them via `const { signIn } = useContext(AuthContext)`
59+ *
60+ * @type {{
61+ * signIn: function({email: *, password: *, onError: *}): void,
62+ * signOut: function({onError: *}): void,
63+ * signUp: function({email: *, password: *, onError: *}): void,
64+ * deleteAccount: function({ onError: * });void
65+ * }}
66+ */
7667 const authContext = useMemo ( ( ) => ( {
7768 signIn : ( { email, password, onError } ) => {
7869 Meteor . loginWithPassword ( email , password , async ( err ) => {
@@ -87,32 +78,42 @@ export const useLogin = () => {
8778 dispatch ( { type, token } )
8879 } )
8980 } ,
90- signOut : ( ) => {
81+ signOut : ( { onError } ) => {
9182 Meteor . logout ( err => {
9283 if ( err ) {
93- // TODO display error, merge into the above workflow
94- return console . error ( err )
84+ return onError ( err )
9585 }
9686 dispatch ( { type : 'SIGN_OUT' } )
9787 } )
9888 } ,
99- signUp : ( { email, password, onError } ) => {
100- Meteor . call ( 'register' , { email, password } , ( err , res ) => {
89+ signUp : ( { email, password, firstName, lastName, onError } ) => {
90+ const signupArgs = { email, password, firstName, lastName, loginImmediately : true }
91+
92+ Meteor . call ( 'registerNewUser' , signupArgs , ( err , credentials ) => {
10193 if ( err ) {
10294 return onError ( err )
10395 }
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- } )
96+
97+ // this sets the { id, token } values internally to make sure
98+ // our calls to Meteor endpoints will be authenticated
99+ Meteor . _handleLoginCallback ( err , credentials )
100+
101+ // from here this is the same routine as in signIn
102+ const token = Meteor . getAuthToken ( )
103+ const type = 'SIGN_IN'
104+ dispatch ( { type, token } )
105+ } )
106+ } ,
107+ deleteAccount : ( { onError } ) => {
108+ Meteor . call ( 'deleteAccount' , ( err ) => {
109+ if ( err ) {
110+ return onError ( err )
111+ }
112+
113+ // removes all auth-based data from client
114+ // as if we would call signOut
115+ Meteor . handleLogout ( )
116+ dispatch ( { type : 'SIGN_OUT' } )
116117 } )
117118 }
118119 } ) , [ ] )
0 commit comments