-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
191 additions
and
66 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
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
7 changes: 7 additions & 0 deletions
7
app/server/src/modules/api/v3/admin/user-resend-verification.http
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,7 @@ | ||
# | ||
POST http://localhost:2024/api/v3/admin/user/resendVerification | ||
Content-Type: application/json | ||
|
||
{ | ||
"accountId": "afe54871-9afd-471f-ad25-331bc7253670" | ||
} |
87 changes: 87 additions & 0 deletions
87
app/server/src/modules/api/v3/admin/user-resend-verification.request.ts
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,87 @@ | ||
import { | ||
RequestType, | ||
RequestMethod, | ||
getResponse, | ||
HttpStatusCode, | ||
getRandomString, | ||
} from "@oh/utils"; | ||
import * as bcrypt from "@da/bcrypt"; | ||
import { RequestKind } from "shared/enums/request.enums.ts"; | ||
import { System } from "modules/system/main.ts"; | ||
import { getEmailByHash } from "shared/utils/account.utils.ts"; | ||
import { hasRequestAccess } from "shared/utils/scope.utils.ts"; | ||
|
||
export const userResendVerificationRequest: RequestType = { | ||
method: RequestMethod.POST, | ||
pathname: "/user/resendVerification", | ||
kind: RequestKind.ADMIN, | ||
func: async (request: Request, url: URL) => { | ||
if (!(await hasRequestAccess({ request, admin: true }))) | ||
return getResponse(HttpStatusCode.FORBIDDEN); | ||
|
||
const { accountId } = await request.json(); | ||
|
||
const account = await System.accounts.get(accountId); | ||
if (!account) return getResponse(HttpStatusCode.FORBIDDEN); | ||
if (account.verified) return getResponse(HttpStatusCode.FORBIDDEN); | ||
|
||
const email = await getEmailByHash(account.emailHash); | ||
|
||
const verifyId = getRandomString(16); | ||
const verifyToken = getRandomString(32); | ||
|
||
const { url: apiUrl } = System.getConfig(); | ||
|
||
const verifyUrl = `${apiUrl}/verify?id=${verifyId}&token=${verifyToken}`; | ||
System.email.send( | ||
email, | ||
"verify your account", | ||
verifyUrl, | ||
`<a href="${verifyUrl}">${verifyUrl}<p/>`, | ||
); | ||
|
||
const { | ||
email: { enabled: isEmailVerificationEnabled }, | ||
times: { accountWithoutVerificationDays }, | ||
} = System.getConfig(); | ||
const expireIn = accountWithoutVerificationDays * 24 * 60 * 60 * 1000; | ||
|
||
await System.db.set( | ||
["accounts", accountId], | ||
{ ...account, createdAt: Date.now() }, | ||
isEmailVerificationEnabled ? { expireIn } : {}, | ||
); | ||
await System.db.set( | ||
["accountsByVerifyId", verifyId], | ||
{ | ||
accountId, | ||
verifyTokensHash: isEmailVerificationEnabled | ||
? bcrypt.hashSync(verifyToken, bcrypt.genSaltSync(8)) | ||
: null, | ||
}, | ||
{ | ||
expireIn, | ||
}, | ||
); | ||
await System.db.set( | ||
["accountsByEmail", account.emailHash], | ||
accountId, | ||
isEmailVerificationEnabled | ||
? { | ||
expireIn, | ||
} | ||
: {}, | ||
); | ||
await System.db.set( | ||
["accountsByUsername", account.username.toLowerCase()], | ||
accountId, | ||
isEmailVerificationEnabled | ||
? { | ||
expireIn, | ||
} | ||
: {}, | ||
); | ||
|
||
return getResponse(HttpStatusCode.OK); | ||
}, | ||
}; |
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 |
---|---|---|
|
@@ -4,4 +4,5 @@ export type Account = { | |
emailHash: string; | ||
passwordHash: string; | ||
createdAt: Date; | ||
verified: boolean; | ||
}; |