-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: express endpoints for mail verification and password reset
- Loading branch information
Showing
8 changed files
with
204 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
declare namespace Express { | ||
export interface Request { | ||
userAgent: string; | ||
ip: string; | ||
infos: { | ||
userAgent: string; | ||
ip: string; | ||
}; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { type Injector } from 'graphql-modules'; | ||
import type { Request, Response, NextFunction } from 'express'; | ||
import validator from 'validator'; | ||
import AccountsPassword from '../accounts-password'; | ||
|
||
function getHtml(title: string, body: string) { | ||
return ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>${title}</title> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> | ||
</head> | ||
<body> | ||
${body} | ||
</body> | ||
</html> | ||
`; | ||
} | ||
|
||
export const infosMiddleware = (req: Request, _res: Response, next: NextFunction) => { | ||
const userAgent = 'userAgent'; | ||
const ip = 'ip'; | ||
req.infos = { | ||
userAgent, | ||
ip, | ||
}; | ||
next(); | ||
}; | ||
|
||
export const verifyEmail = (injector: Injector) => async (req: Request, res: Response) => { | ||
try { | ||
const { token } = req.params; | ||
if (token == null) { | ||
throw new Error('Token is missing'); | ||
} | ||
await injector.get(AccountsPassword).verifyEmail(token); | ||
res.send( | ||
getHtml( | ||
'Email successfully verified', | ||
` | ||
<h3>The email address has been successfully verified.</h3> | ||
` | ||
) | ||
); | ||
} catch (err: any) { | ||
res.send( | ||
//codeql[js/xss-through-exception] | ||
getHtml( | ||
'Email verification error', | ||
` | ||
<h3>The email address couldn't be verified: ${err.message ?? 'unknown error'}</h3> | ||
` | ||
) | ||
Check warning Code scanning / CodeQL Exception text reinterpreted as HTML Medium Exception text Error loading related location Loading |
||
); | ||
} | ||
}; | ||
|
||
export const resetPassword = (injector: Injector) => async (req: Request, res: Response) => { | ||
try { | ||
const { token, newPassword } = req.body; | ||
if (token == null) { | ||
throw new Error('Token is missing'); | ||
} | ||
if (newPassword == null) { | ||
throw new Error('New password is missing'); | ||
} | ||
await injector.get(AccountsPassword).resetPassword(token, newPassword, req.infos); | ||
res.send( | ||
getHtml( | ||
'Password successfully changed', | ||
` | ||
<h3>The password has been successfully changed.</h3> | ||
` | ||
) | ||
); | ||
} catch (err: any) { | ||
res.send( | ||
getHtml( | ||
'Password reset error', | ||
` | ||
<h3>The password couldn't be changed: ${err.message ?? 'unknown error'}</h3> | ||
` | ||
) | ||
Check warning Code scanning / CodeQL Exception text reinterpreted as HTML Medium Exception text Error loading related location Loading |
||
); | ||
} | ||
}; | ||
|
||
export const resetPasswordForm = (req: Request, res: Response): Response => | ||
res.send( | ||
getHtml( | ||
'Reset password', | ||
` | ||
<div class="container"> | ||
<h1>Reset your password</h1> | ||
<form action="/resetPassword" method="POST"> | ||
<input type="hidden" name="token" value=${validator.escape(req.params.token)} /> | ||
<div class="form-group"> | ||
<label for="newPassword">New password</label> | ||
<input type="text" class="form-control" id="newPassword" value="" placeholder="Enter your new password" name="newPassword"> | ||
</div> | ||
<button type="submit" class="btn btn-primary">Submit</button> | ||
</form> | ||
` | ||
) | ||
); |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './express'; |
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