Fix logging in

This commit is contained in:
pikiou 2022-01-09 04:39:43 +01:00
parent 5cbb5811ec
commit d5ae2fe5f8

View File

@ -24,7 +24,8 @@ export const volunteerAdd = expressAccessor.add()
export const volunteerSet = expressAccessor.set()
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)
if (!volunteer) {
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 } = {}
export const volunteerForgot = expressAccessor.set(async (list: Volunteer[], body: RequestBody) => {
const volunteer = getByEmail(list, body.email)
if (!volunteer) {
throw Error("Il n'y a aucun bénévole avec cet email")
export const volunteerForgot = expressAccessor.set(
async (list: Volunteer[], bodyArray: RequestBody) => {
const [body] = bodyArray
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(
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)
if (!volunteer) {
throw Error(`Il n'y a aucun bénévole avec cet identifiant ${id}`)
}
const newVolunteer = _.cloneDeep(volunteer)
_.assign(newVolunteer, _.pick(body[1], _.keys(newVolunteer)))
_.assign(newVolunteer, _.pick(notifChanges, _.keys(newVolunteer)))
return {
toDatabase: newVolunteer,