-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
132 lines (122 loc) · 3.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const fbAdmin = require('../../firebase');
const express = require('express');
const router = express.Router();
async function checkUserInFirebase(email) {
return new Promise((resolve) => {
fbAdmin
.auth()
.getUserByEmail(email)
.then((user) => {
resolve(true);
})
.catch((err) => {
resolve(false);
});
});
}
/**
* @swagger
* paths:
* /signup:
* post:
* summary: Create a new user if the given credentials are valid
* tags: [Auth]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* properties:
* username:
* type: string
* email:
* type: string
* password:
* type: string
* description: The password must have at least 8 characters
* responses:
* 200:
* description: Successful request, a new user is created and a user token is returned.
* content:
* application/json:
* schema:
* properties:
* message:
* type: string
* default: 'ok'
* status:
* type: integer
* default: 200
* user_token:
* type: string
* description: The password must have at least 8 characters
* 400:
* description: Invalid request, the given fields are not valid or the user already exists.
* content:
* application/json:
* schema:
* properties:
* message:
* type: string
* default: '[ERROR_MESSAGE]'
* status:
* type: integer
* default: 400
*/
router.post('/signup', async (req, res) => {
console.log(req.body);
try {
res.set('Access-Control-Allow-Origin', '*');
const { email, username, password } = req.body;
// check the password length is valid (< 8)
if (password.length < 8) {
return res.status(400).json({
message: 'Password must be at least 8 characters',
status: 400,
});
}
// check the email format is valid
const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
if (!emailRegex.test(email)) {
return res.status(400).json({
message: 'Email that was provided is invalid format',
status: 400,
});
}
// check the user name format is valid
const usernameRegex = /^[a-zA-Z0-9._-]{4,}$/;
if (!usernameRegex.test(username)) {
return res.status(400).json({
message: 'Username is invalid format',
status: 400,
});
}
const alreadyExist = await checkUserInFirebase(email);
if (alreadyExist) {
return res.status(400).json({
message: 'User already exists with the given email address',
status: 400,
});
}
const user = await fbAdmin.auth().createUser({
username,
email,
password,
});
// create a user token
const userToken = await fbAdmin.auth().createCustomToken(user.uid);
// return successful registration response
return res.status(200).json({
message: 'ok',
status: 200,
user_token: userToken,
});
} catch (error) {
const errorMessage = error.message;
return res.status(400).json({
message: errorMessage,
status: 400,
});
}
});
module.exports = router;