-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.js
More file actions
173 lines (135 loc) · 3.93 KB
/
Copy pathuser.js
File metadata and controls
173 lines (135 loc) · 3.93 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
var bcrypt = require('bcryptjs')
const express = require('express')
const rUser = express.Router()
//importo i file necessari
const { mailValidation } = require("../Validation/email.js")
const { passwordValidation } = require("../Validation/password.js")
const { db } = require("../dbApi/index.js")
const { createToken, validateToken } = require("./jwt.js")
const { registerUser } = require('../dbApi/Methods/User/register.js')
//do a login
rUser.post("/login", async (req, res) => {
let mail = req.body.mail
let psw = req.body.psw
//controllo se sono stati mandati mail e password
if (mail == undefined || psw == undefined) {
res.status(500).send("non sono state mandate mail e/o password")
return
}
let result = undefined
//recupero i dati utente dal db
await db.connect()
result = await db.findUser(mail)
console.log("result:")
console.log(result)
let hash
//mi assicuro che ci sia il campo hash nella risposta
try {
hash = result["hash"]
} catch (error) {
res.status(500).send("Utente non presente")
return
}
//controllo la password criptata con quella inviata
if (!bcrypt.compareSync(psw, hash)) {
res.status(500).send("Password non corretta")
return
}
let objs = JSON.parse(JSON.stringify(result));
token = createToken(objs)
if (!token) {
res.status(500).send("Token not created")
return
}
//se tutto va a buon fine mando i dati al richiedente
res.status(200).send({ token, result })
return
})
//update
rUser.post("/update", async (req, res) => {
let token = req.headers.token
let validate = validateToken(token)
if (!validate) {
res.status(500).send("Token inviato non corretto")
return
}
let object = req.body
let mail = req.body.mail
delete object.mail
if (object == undefined) {
res.status(500).send("Non sono stati mandati i dati per modificare l'utente")
return
}
await db.connect()
let result = await db.updateUser(object, mail)
if (!result) {
res.status(500).send("Errore durante l'update dell utente")
return
}
res.status(200).send("Utente modificato con successo")
})
//register login
rUser.post("/", async (req, res) => {
let usr
let mail
let psw
let saltRounds = bcrypt.genSaltSync(10);
let imgProfile
let dataRegistration
//controllo che siano stati inviati tutti i dati richiesti
try {
let date = req.body
usr = date["usr"]
mail = date["mail"]
psw = date["psw"]
imgProfile = date["profile"]
dataRegistration = date["dataR"]
} catch (error) {
res.status(500).send("Non sono stati mandati i dati necessare per registrare l'account")
return
}
//valido la mail
if (!mailValidation(mail)) {
res.status(500).send("mail inviata non corretta, controllare")
return
}
//valido la password
if (!passwordValidation(psw)) {
res.status(500).send("Password inserita non abbastanza sicura, deve avere tra i 6 e i 15 caratteri, avere caratteri speciali (@,!,$,%), avere numeri e lettere maiuscole")
return
}
//crypted password
let hashPassword = bcrypt.hashSync(psw, saltRounds);
//controllo se esiste già un utente con quella mail nel db
await db.connect()
let result = await db.findUser(mail)
console.log("result:")
console.log(result)
//controllo che non esista nessun utente con quella mail
if (result != undefined) {
res.status(500).send("é già presente un utente con la mail " + mail)
return
}
//creo un nuovo utente
let newUser = {
username: usr,
email: mail,
profile_image_url: imgProfile,
salt: saltRounds,
hash: hashPassword,
dataR: dataRegistration
}
//inserisco l'utente nel db
let data = await db.registerUser(newUser)
if (!data) {
res.status(500).send("Utente non registrato")
return
}
let token = createToken(newUser)
if (!token) {
res.status(500).send("Token not created")
return
}
res.status(200).send({ token, newUser })
})
module.exports = { rUser }