8
8
9
9
// Class
10
10
class auth {
11
+
11
12
// Constructor
12
13
constructor ( handlers ) {
14
+
13
15
// Configuration
14
16
this . post = handlers . post ;
15
17
this . duplex = handlers . duplex ;
18
+
16
19
}
17
20
18
- async login ( email , password ) {
21
+ async login ( email , { password, passwordless = true , create = true } = { } ) {
22
+
19
23
if ( typeof window === "undefined" )
24
+
20
25
// Not supported warning
21
26
return console . warn ( "The login feature is not available in node. Get auth token from Grandeur Dashboard and use the token() function." ) ;
22
27
23
28
// This function sends "login a user" request with required data to the server
24
29
// Submit the request and wait for request to be processed
25
- var res = await this . post . send ( "/auth/login" , { email : email , password : password } ) ;
30
+ var res = await this . post . send ( "/auth/login" , { email : email , password : password , options : { passwordless , create } } ) ;
26
31
27
32
// then if the login process completed successfully
28
33
if ( res . code === "AUTH-ACCOUNT-LOGGEDIN" ) {
34
+
29
35
// Then we will set the token to the local storage
30
36
if ( typeof window !== "undefined" ) localStorage . setItem ( `grandeur-auth-${ this . post . config . apiKey } ` , res . token ) ;
31
37
32
38
// Update the configuration
33
39
this . post . config . token = res . token ;
40
+
41
+ }
42
+
43
+ // But in case it was a passwordless login
44
+ // where we are required to resolve the otp
45
+ if ( res . code === "CODE-SENT" ) {
46
+
47
+ // We will return the response with a confirm function
48
+ return {
49
+
50
+ code : "CODE-SENT" ,
51
+
52
+ // Function will take tehe token from user and attempt to confirm email
53
+ confirm : async ( code ) => {
54
+
55
+ // Take code and token
56
+ var response = await this . post . send ( "/auth/login" , { token : res . token , code : code , options : { passwordless, create } } ) ;
57
+
58
+ // Check for response code
59
+ if ( response . code === "AUTH-ACCOUNT-LOGGEDIN" ) {
60
+
61
+ // Set the token in localstorage for future use
62
+ if ( typeof window !== "undefined" ) localStorage . setItem ( `grandeur-auth-${ this . post . config . apiKey } ` , response . token ) ;
63
+
64
+ // Load configuration
65
+ this . post . config . token = response . token ;
66
+
67
+ }
68
+
69
+ // Resolve promise
70
+ return response ;
71
+
72
+ }
73
+
74
+ }
75
+
34
76
}
35
77
36
78
// Resolve promise
37
79
return res ;
80
+
38
81
}
39
82
40
- async register ( email , password , displayName , phone ) {
83
+ async register ( email , password ) {
84
+
41
85
if ( typeof window === "undefined" )
86
+
42
87
// Not supported warning
43
88
return console . warn ( "The login feature is not available in node. Get auth token from Grandeur Dashboard and use the token() function." ) ;
44
89
45
90
// This function sends "register" request with provided data to the server
46
91
// submit the request
47
- try {
48
- // Get the response
49
- var res = await this . post . send ( "/auth/register" , { email : email , password : password , displayName : displayName , phone : phone } ) ;
50
-
51
- // and return a confirmation function if token sent
52
- if ( res . code === "PHONE-CODE-SENT" )
53
- return {
54
- code : res . code ,
55
- message : res . message ,
56
-
57
- // Append confirm function
58
- confirm : async ( verificationCode ) => {
59
- // Confirmation function will get the token from the response object received
60
- // earlier as a result of register request with user data and will get code from
61
- // the user via the argument and then using the post handler function will submit
62
- // the request again
63
- var response = await this . post . send ( "/auth/register" , { token : res . token , verificationCode : verificationCode } ) ;
64
-
65
- // Check for response code
66
- if ( response . code === "AUTH-ACCOUNT-REGISTERED" ) {
67
- // Set the token in localstorage for future use
68
- if ( typeof window !== "undefined" ) localStorage . setItem ( `grandeur-auth-${ this . post . config . apiKey } ` , res . token ) ;
69
-
70
- // Load configuration
71
- this . post . config . token = res . token ;
72
- }
73
-
74
- // Resolve promise
75
- return response ;
76
- } ,
77
- } ;
78
- else return res ;
79
- } catch ( err ) {
80
- // Got an error then just throw it
81
- throw err ;
82
- }
83
- }
84
92
85
- async updateProfile ( displayName , displayPicture , phone ) {
86
- // This function sends "updateProfile" request with provided data to the server
87
- // submit the request
88
- try {
89
- // Get the response
90
- var res = await this . post . send ( "/auth/updateProfile" , { displayName : displayName , phone : phone , displayPicture : displayPicture } ) ;
91
-
92
- // and return a confirmation function if token sent
93
- if ( res . code === "PHONE-CODE-SENT" )
94
- return {
95
- code : res . code ,
96
- message : res . message ,
97
-
98
- // Append confirm function
99
- confirm : ( verificationCode ) => {
100
- // Confirmation function will get the token from the response object received
101
- // earlier as a result of register request with user data and will get code from
102
- // the user via the argument and then using the post handler function will submit
103
- // the request again
104
- return this . post . send ( "/auth/updateProfile" , { token : res . token , verificationCode : verificationCode } ) ;
105
- } ,
106
- } ;
107
- else return res ;
108
- } catch ( err ) {
109
- // Got an error then just throw it
110
- throw err ;
111
- }
112
- }
93
+ // Get the response
94
+ var res = await this . post . send ( "/auth/register" , { email : email , password : password } ) ;
113
95
114
- async forgotPassword ( email ) {
115
- // This function sends "forgotPassword" request with provided data to the server
116
- // submit the request
117
- try {
118
- // Get the response
119
- var res = await this . post . send ( "/auth/forgotPassword" , { email : email } ) ;
120
-
121
- // and return a confirmation function if token sent
122
- if ( res . code === "PHONE-CODE-SENT" )
123
- return {
124
- code : res . code ,
125
- message : res . message ,
126
-
127
- // Append confirm function
128
- confirm : ( verificationCode , password ) => {
129
- // Confirmation function will get the token from the response object received
130
- // earlier as a result of register request with user data and will get code from
131
- // the user via the argument and then using the post handler function will submit
132
- // the request again
133
- return this . post . send ( "/auth/forgotPassword" , { token : res . token , verificationCode : verificationCode , password : password } ) ;
134
- } ,
135
- } ;
136
- else return res ;
137
- } catch ( err ) {
138
- // Got an error then just throw it
139
- throw err ;
140
- }
96
+ // and return a confirmation function if token sent
97
+ if ( res . code === "CODE-SENT" )
98
+
99
+ return {
100
+ code : res . code ,
101
+
102
+ // Function will take tehe token from user and attempt to confirm email
103
+ confirm : async ( code ) => {
104
+
105
+ // Take code and token
106
+ var response = await this . post . send ( "/auth/register" , { token : res . token , code : code } ) ;
107
+
108
+ // Check for response code
109
+ if ( response . code === "AUTH-ACCOUNT-REGISTERED" ) {
110
+
111
+ // Set the token in localstorage for future use
112
+ if ( typeof window !== "undefined" ) localStorage . setItem ( `grandeur-auth-${ this . post . config . apiKey } ` , response . token ) ;
113
+
114
+ // Load configuration
115
+ this . post . config . token = response . token ;
116
+
117
+ }
118
+
119
+ // Resolve promise
120
+ return response ;
121
+
122
+ }
123
+ }
124
+
125
+ return res ;
141
126
}
142
127
143
- async changePassword ( password ) {
144
- // This function sends "changePassword" request with provided data to the server
128
+ async reset ( email ) {
129
+
130
+ // This function sends reset request with provided data to the server
145
131
// submit the request
146
- try {
147
- // Get the response
148
- var res = await this . post . send ( "/auth/changePassword" , { password : password } ) ;
149
-
150
- // and return a confirmation function if token sent
151
- if ( res . code === "PHONE-CODE-SENT" )
152
- return {
153
- code : res . code ,
154
- message : res . message ,
155
-
156
- // Append confirm function
157
- confirm : ( verificationCode ) => {
158
- // Confirmation function will get the token from the response object received
159
- // earlier as a result of register request with user data and will get code from
160
- // the user via the argument and then using the post handler function will submit
161
- // the request again
162
- return this . post . send ( "/auth/changePassword" , { token : res . token , verificationCode : verificationCode } ) ;
163
- } ,
164
- } ;
165
- else return res ;
166
- } catch ( err ) {
167
- // Got an error then just throw it
168
- throw err ;
169
- }
170
- }
171
132
172
- isAuthenticated ( ) {
173
- // This function sends "check if a user's logged in" request with required data to the server
174
- return this . post . send ( "/auth/protectedpage" , { } ) ;
133
+ // Get the response
134
+ var res = await this . post . send ( "/auth/reset" , { email : email } ) ;
135
+
136
+ // and return a confirmation function if token sent
137
+ if ( res . code === "CODE-SENT" )
138
+
139
+ return {
140
+
141
+ code : "CODE-SENT" ,
142
+
143
+ // Append confirm function
144
+ confirm : ( code , password ) => {
145
+
146
+ // Send request
147
+ return this . post . send ( "/auth/reset" , { token : res . token , code : code , password : password } ) ;
148
+ } ,
149
+ }
150
+
151
+
152
+ return res ;
175
153
}
176
154
177
155
ping ( ) {
@@ -185,31 +163,27 @@ class auth {
185
163
}
186
164
187
165
async token ( token ) {
188
- // And run this in try catch
189
- try {
190
- // Send ping request to the server with this token
191
- var res = await this . post . send ( "/auth/ping" , { } , token ) ;
192
166
193
- // If the token is valid
194
- if ( res . code === "AUTH-AUTHORIZED" ) {
195
- // Update configuration
196
- this . post . config . token = token ;
167
+ // And run this in try catch
168
+ // Send ping request to the server with this token
169
+ var res = await this . post . send ( "/auth/ping" , { } , token ) ;
197
170
198
- // And technically, we should reconnect
199
- this . duplex . reconnect ( ) ;
171
+ // If the token is valid
172
+ if ( res . code === "AUTH-AUTHORIZED" ) {
173
+ // Update configuration
174
+ this . post . config . token = token ;
200
175
201
- // Return response
202
- return res ;
203
- }
176
+ // And technically, we should reconnect
177
+ this . duplex . reconnect ( ) ;
204
178
205
- // Or return error
206
- throw {
207
- code : "TOKEN-INVALID" ,
208
- } ;
209
- } catch ( error ) {
210
- // Send invalid token error
211
- throw error ;
179
+ // Return response
180
+ return res ;
212
181
}
182
+
183
+ // Or return error
184
+ throw {
185
+ code : "TOKEN-INVALID" ,
186
+ } ;
213
187
}
214
188
}
215
189
0 commit comments