Skip to content

Commit 613a753

Browse files
committed
Migrated auth
1 parent 9e18e4d commit 613a753

File tree

9 files changed

+160
-167
lines changed

9 files changed

+160
-167
lines changed

.DS_Store

0 Bytes
Binary file not shown.

dist/grandeur-cjs.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/grandeur-react.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/grandeur.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 1 addition & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@
3636
"fetch-ponyfill": "^7.1.0",
3737
"hi-base32": "^0.5.1",
3838
"isomorphic-ws": "^5.0.0",
39-
"localStorage": "^1.0.4",
40-
"totp-generator": "^0.0.13",
41-
"ws": "^8.13.0"
39+
"totp-generator": "^0.0.13"
4240
},
4341
"files": [
4442
"dist"

src/auth.js

Lines changed: 119 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -8,170 +8,148 @@
88

99
// Class
1010
class auth {
11+
1112
// Constructor
1213
constructor(handlers) {
14+
1315
// Configuration
1416
this.post = handlers.post;
1517
this.duplex = handlers.duplex;
18+
1619
}
1720

18-
async login(email, password) {
21+
async login(email, { password, passwordless = true, create = true } = { }) {
22+
1923
if (typeof window === "undefined")
24+
2025
// Not supported warning
2126
return console.warn("The login feature is not available in node. Get auth token from Grandeur Dashboard and use the token() function.");
2227

2328
// This function sends "login a user" request with required data to the server
2429
// 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 } });
2631

2732
// then if the login process completed successfully
2833
if (res.code === "AUTH-ACCOUNT-LOGGEDIN") {
34+
2935
// Then we will set the token to the local storage
3036
if (typeof window !== "undefined") localStorage.setItem(`grandeur-auth-${this.post.config.apiKey}`, res.token);
3137

3238
// Update the configuration
3339
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+
3476
}
3577

3678
// Resolve promise
3779
return res;
80+
3881
}
3982

40-
async register(email, password, displayName, phone) {
83+
async register(email, password) {
84+
4185
if (typeof window === "undefined")
86+
4287
// Not supported warning
4388
return console.warn("The login feature is not available in node. Get auth token from Grandeur Dashboard and use the token() function.");
4489

4590
// This function sends "register" request with provided data to the server
4691
// 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-
}
8492

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 });
11395

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;
141126
}
142127

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
145131
// 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-
}
171132

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;
175153
}
176154

177155
ping() {
@@ -185,31 +163,27 @@ class auth {
185163
}
186164

187165
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);
192166

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);
197170

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;
200175

201-
// Return response
202-
return res;
203-
}
176+
// And technically, we should reconnect
177+
this.duplex.reconnect();
204178

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;
212181
}
182+
183+
// Or return error
184+
throw {
185+
code: "TOKEN-INVALID",
186+
};
213187
}
214188
}
215189

0 commit comments

Comments
 (0)