mirror of
https://github.com/Paris-est-Ludique/intranet.git
synced 2025-06-10 01:24:20 +02:00
Fix logging in
This commit is contained in:
parent
5cbb5811ec
commit
d5ae2fe5f8
@ -24,7 +24,8 @@ export const volunteerAdd = expressAccessor.add()
|
|||||||
export const volunteerSet = expressAccessor.set()
|
export const volunteerSet = expressAccessor.set()
|
||||||
|
|
||||||
export const volunteerLogin = expressAccessor.get(
|
export const volunteerLogin = expressAccessor.get(
|
||||||
async (list: Volunteer[], body: RequestBody): Promise<VolunteerLogin> => {
|
async (list: Volunteer[], bodyArray: RequestBody): Promise<VolunteerLogin> => {
|
||||||
|
const [body] = bodyArray
|
||||||
const volunteer = getByEmail(list, body.email)
|
const volunteer = getByEmail(list, body.email)
|
||||||
if (!volunteer) {
|
if (!volunteer) {
|
||||||
throw Error("Il n'y a aucun bénévole avec cet email")
|
throw Error("Il n'y a aucun bénévole avec cet email")
|
||||||
@ -55,45 +56,53 @@ export const volunteerLogin = expressAccessor.get(
|
|||||||
)
|
)
|
||||||
|
|
||||||
const lastForgot: { [id: string]: number } = {}
|
const lastForgot: { [id: string]: number } = {}
|
||||||
export const volunteerForgot = expressAccessor.set(async (list: Volunteer[], body: RequestBody) => {
|
export const volunteerForgot = expressAccessor.set(
|
||||||
const volunteer = getByEmail(list, body.email)
|
async (list: Volunteer[], bodyArray: RequestBody) => {
|
||||||
if (!volunteer) {
|
const [body] = bodyArray
|
||||||
throw Error("Il n'y a aucun bénévole avec cet email")
|
const volunteer = getByEmail(list, body.email)
|
||||||
|
if (!volunteer) {
|
||||||
|
throw Error("Il n'y a aucun bénévole avec cet email")
|
||||||
|
}
|
||||||
|
const newVolunteer = _.cloneDeep(volunteer)
|
||||||
|
|
||||||
|
const now = +new Date()
|
||||||
|
const timeSinceLastSent = now - lastForgot[volunteer.id]
|
||||||
|
if (timeSinceLastSent < 2 * 60 * 1000) {
|
||||||
|
throw Error(
|
||||||
|
"Un email t'a déjà été envoyé avec un nouveau mot de passe. Es-tu sûr qu'il n'est pas dans tes spams ?"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
lastForgot[volunteer.id] = now
|
||||||
|
|
||||||
|
const password = generatePassword()
|
||||||
|
const passwordHash = await bcrypt.hash(password, 10)
|
||||||
|
newVolunteer.password2 = passwordHash
|
||||||
|
|
||||||
|
await sendForgetEmail(volunteer.email, password)
|
||||||
|
|
||||||
|
return {
|
||||||
|
toDatabase: newVolunteer,
|
||||||
|
toCaller: {
|
||||||
|
message: `Un nouveau mot de passe t'a été envoyé par email. Regarde bien dans tes spams, ils pourrait y être :/`,
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const newVolunteer = _.cloneDeep(volunteer)
|
)
|
||||||
|
|
||||||
const now = +new Date()
|
|
||||||
const timeSinceLastSent = now - lastForgot[volunteer.id]
|
|
||||||
if (timeSinceLastSent < 2 * 60 * 1000) {
|
|
||||||
throw Error(
|
|
||||||
"Un email t'a déjà été envoyé avec un nouveau mot de passe. Es-tu sûr qu'il n'est pas dans tes spams ?"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
lastForgot[volunteer.id] = now
|
|
||||||
|
|
||||||
const password = generatePassword()
|
|
||||||
const passwordHash = await bcrypt.hash(password, 10)
|
|
||||||
newVolunteer.password2 = passwordHash
|
|
||||||
|
|
||||||
await sendForgetEmail(volunteer.email, password)
|
|
||||||
|
|
||||||
return {
|
|
||||||
toDatabase: newVolunteer,
|
|
||||||
toCaller: {
|
|
||||||
message: `Un nouveau mot de passe t'a été envoyé par email. Regarde bien dans tes spams, ils pourrait y être :/`,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
export const volunteerNotifsSet = expressAccessor.set(
|
export const volunteerNotifsSet = expressAccessor.set(
|
||||||
async (list: Volunteer[], body: RequestBody, id: number) => {
|
async (list: Volunteer[], body: RequestBody, id: number) => {
|
||||||
|
const requestedId = +body[0]
|
||||||
|
if (requestedId !== id) {
|
||||||
|
throw Error(`On ne peut acceder qu'à ses propres notifs`)
|
||||||
|
}
|
||||||
|
const notifChanges = body[1]
|
||||||
const volunteer = list.find((v) => v.id === id)
|
const volunteer = list.find((v) => v.id === id)
|
||||||
if (!volunteer) {
|
if (!volunteer) {
|
||||||
throw Error(`Il n'y a aucun bénévole avec cet identifiant ${id}`)
|
throw Error(`Il n'y a aucun bénévole avec cet identifiant ${id}`)
|
||||||
}
|
}
|
||||||
const newVolunteer = _.cloneDeep(volunteer)
|
const newVolunteer = _.cloneDeep(volunteer)
|
||||||
|
|
||||||
_.assign(newVolunteer, _.pick(body[1], _.keys(newVolunteer)))
|
_.assign(newVolunteer, _.pick(notifChanges, _.keys(newVolunteer)))
|
||||||
|
|
||||||
return {
|
return {
|
||||||
toDatabase: newVolunteer,
|
toDatabase: newVolunteer,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user