This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
-
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.
* Update tokens format * Rename interface * Diffrent keys for each tokens & cleanup * Fix error handling & response format * Update src/services/auth.service.ts
- Loading branch information
1 parent
7b4f643
commit ca7750c
Showing
8 changed files
with
63 additions
and
50 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
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,9 +1,9 @@ | ||
import crypto from 'crypto-js'; | ||
|
||
export const encryptMessage = (text: string) => { | ||
return crypto.AES.encrypt(text, process.env.SECRET_ENCRYPTION_KEY).toString(); | ||
return crypto.AES.encrypt(text, process.env.CHAT_ENCRYPTION_SECRET_KEY).toString(); | ||
}; | ||
|
||
export const decryptMessage = (text: string) => { | ||
return crypto.AES.decrypt(text, process.env.SECRET_ENCRYPTION_KEY).toString(crypto.enc.Utf8); | ||
return crypto.AES.decrypt(text, process.env.CHAT_ENCRYPTION_SECRET_KEY).toString(crypto.enc.Utf8); | ||
}; |
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,39 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import client from '@/prisma/client'; | ||
|
||
import { DataStoredInToken, UserTokens } from '@/interfaces/auth.interface'; | ||
import { User } from '@prisma/client'; | ||
|
||
const ONE_DAY_IN_SECONDS = 24 * 60 * 60; | ||
const ONE_MONTH_IN_SECONDS = 30 * 24 * 60 * 60; | ||
|
||
export async function displayNameToUsername(displayName: string): Promise<string> { | ||
const username: string = displayName.toLowerCase().trim().normalize('NFD'); | ||
const cleanedUsername = username | ||
.replace(/[\u0300-\u036f]/g, '') | ||
.replace(/\s/g, '_') | ||
.replace(/[^\w\s]/gi, ''); | ||
|
||
let count = 0; | ||
let uniqueUsername = cleanedUsername; | ||
while (await client.user.findUnique({ where: { username: uniqueUsername } })) { | ||
uniqueUsername = username + count.toString(); | ||
count++; | ||
} | ||
return uniqueUsername; | ||
} | ||
|
||
export function createUserToken(user: User): UserTokens { | ||
const dataStoredInToken: DataStoredInToken = { userId: user.id }; | ||
const accessKey = process.env.ACCESS_SECRET_KEY; | ||
const refreshKey = process.env.REFRESH_SECRET_KEY; | ||
|
||
const accessToken = jwt.sign(dataStoredInToken, accessKey, { expiresIn: ONE_DAY_IN_SECONDS }); | ||
const refreshToken = jwt.sign(dataStoredInToken, refreshKey, { expiresIn: ONE_MONTH_IN_SECONDS }); | ||
|
||
return { | ||
accessToken, | ||
refreshToken, | ||
expires: ONE_DAY_IN_SECONDS, | ||
}; | ||
} |