-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
work on signup and password recovery, change build script
- Loading branch information
Showing
7 changed files
with
98 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,61 @@ | ||
import {FastifyInstance} from 'fastify'; | ||
import {randomBytes} from 'crypto'; | ||
import {passRecoveryScheme} from '@/components/users/passRecovery.scheme'; | ||
import {passResetScheme, usersPasswordScheme} from '@/components/users/passRecovery.scheme' | ||
import {createTransport} from 'nodemailer'; | ||
import {SMTPOpt} from '@/assist/mail'; | ||
import {sha256} from '@/components/auth/assistant'; | ||
|
||
interface IBody { | ||
email?: string, | ||
code?: string, | ||
password?: string, | ||
confirmPassword?: string | ||
interface IBodyPassReset { | ||
email: string, | ||
} | ||
interface IBodyUsersPassword { | ||
email: string, | ||
code: string, | ||
password: string, | ||
confirmPassword: string | ||
} | ||
export const passRecovery = async (server: FastifyInstance) => { | ||
server.patch<{Body: IBody}>( | ||
'/users/password-recovery', | ||
{schema: passRecoveryScheme}, | ||
server.patch<{Body: IBodyPassReset}>( | ||
'/password-reset', | ||
{schema: passResetScheme}, | ||
async (req, reply) => { | ||
const {email, code, password, confirmPassword} = req.body; | ||
const {email} = req.body; | ||
|
||
if (email) { | ||
const recoveryCode = randomBytes(48).toString('hex').substring(0, 63); | ||
const {rowCount} = await server.pg.query('UPDATE root.users SET code = $1 WHERE email = $2', [recoveryCode, email]); | ||
const recoveryCode = Math.floor(Math.random() * 10000000).toString(); | ||
const {rowCount} = await server.pg.query('UPDATE root.users SET code = $1 WHERE email = $2', [recoveryCode, email]); | ||
|
||
if (rowCount) { | ||
const transporter = createTransport(SMTPOpt); | ||
const mailOptions = { | ||
to: email, | ||
subject: 'Password recovery', | ||
html: `<p>Your recovery code: <br> <b>${recoveryCode}</b> <br>Please copy it to the app</p>` | ||
}; | ||
const sent = await transporter.sendMail(mailOptions); | ||
if (rowCount) { | ||
const transporter = createTransport(SMTPOpt); | ||
const mailOptions = { | ||
to: email, | ||
subject: 'Password recovery', | ||
html: `<p>Your recovery code: <b>${recoveryCode}</b> <br>Please copy it to the app</p>` | ||
}; | ||
const sent = await transporter.sendMail(mailOptions); | ||
|
||
if (sent) { | ||
return reply.status(200).send('An email has been sent to your email address'); | ||
} else { | ||
return reply.status(500).send('Couldn\'t send email.'); | ||
} | ||
if (sent) { | ||
return reply.status(200).send('An email has been sent to your email address'); | ||
} else { | ||
return reply.status(500).send('Failed to write to the database'); | ||
return reply.status(500).send('Couldn\'t send email.'); | ||
} | ||
} else { | ||
return reply.status(500).send('Failed to write to the database'); | ||
} | ||
}); | ||
|
||
server.patch<{Body: IBodyUsersPassword}>( | ||
'/users/password', | ||
{schema: usersPasswordScheme}, | ||
async (req, reply) => { | ||
const {email, code, password} = req.body; | ||
|
||
if (code && password && confirmPassword) { | ||
const hash = sha256(password); | ||
const {rowCount} = await server.pg.query('UPDATE root.users SET code = NULL, password_hash = $1 WHERE code = $2', [hash, code]); | ||
const hash = sha256(password); | ||
const {rowCount} = await server.pg.query('UPDATE root.users SET code = NULL, password_hash = $1 WHERE code = $2 AND email=$3', [hash, code, email]); | ||
|
||
if (rowCount) { | ||
return reply.send('You have successfully changed your password!'); | ||
} else { | ||
return reply.status(500).send('Something went wrong. Check the code'); | ||
} | ||
if (rowCount) { | ||
return reply.send('You have successfully changed your password!'); | ||
} else { | ||
return reply.status(500).send('Something went wrong. Check the code'); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters