-
-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: express endpoints for mail verification and password reset #1262
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@accounts/password': patch | ||
--- | ||
dependencies updates: | ||
- Added dependency [`validator@^13.11.0` ↗︎](https://www.npmjs.com/package/validator/v/13.11.0) (to `dependencies`) |
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,109 @@ | ||
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> | ||
` | ||
) | ||
Comment on lines
+51
to
+56
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) { | ||
//codeql[js/xss-through-exception] | ||
res.send( | ||
getHtml( | ||
'Password reset error', | ||
` | ||
<h3>The password couldn't be changed: ${err.message ?? 'unknown error'}</h3> | ||
` | ||
) | ||
Comment on lines
+82
to
+87
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Missing rate limiting High