generated from phucvinh57/fastify-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(env.ts): accessing env variable via 'process.env' and implementin…
…g Google Oauth Previously, encountered an error when accessing env variables from `envs` outside `env.ts` in the envalid library. As a temporary solution, switched to using `process.env`. Additionally, started implementing the basics of Google OAuth but haven't verified it yet. BREAKING CHANGE: Changing to using process.env instead of envs (envalid). Need to fix later.
- Loading branch information
Showing
17 changed files
with
401 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-- AlterTable | ||
ALTER TABLE "User" ALTER COLUMN "userName" DROP NOT NULL, | ||
ALTER COLUMN "password" DROP NOT NULL; |
8 changes: 8 additions & 0 deletions
8
prisma/migrations/20231111080227_set_email_is_unique/migration.sql
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,8 @@ | ||
/* | ||
Warnings: | ||
- A unique constraint covering the columns `[email]` on the table `User` will be added. If there are existing duplicate values, this will fail. | ||
*/ | ||
-- CreateIndex | ||
CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); |
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,28 +1,50 @@ | ||
import { logger } from '@utils'; | ||
import { config as configEnv } from 'dotenv'; | ||
import { str, num, cleanEnv } from 'envalid'; | ||
import { str, url, host, port, cleanEnv } from 'envalid'; | ||
import path from 'path'; | ||
|
||
configEnv(); | ||
const envFilePath = path.join(process.cwd(), '.env'); | ||
if (!envFilePath) { | ||
logger.error('.env file not found.'); | ||
process.exit(1); // Exit the process with an error code | ||
} | ||
configEnv({ path: envFilePath }); | ||
|
||
export const envs = cleanEnv(process.env, { | ||
NODE_ENV: str<NodeEnv>({ | ||
devDefault: 'development', | ||
choices: ['development', 'test', 'production'] | ||
}), | ||
JWT_SECRET: str(), | ||
COOKIE_SECRET: str(), | ||
CORS_WHITE_LIST: str(), | ||
MINIO_URL: str(), | ||
MINIO_SERVER_ENDPOINT: str(), | ||
MINIO_PORT: num(), | ||
MINIO_ACCESS_KEY: str(), | ||
MINIO_SECRET_KEY: str(), | ||
MINIO_BUCKET_NAME: str(), | ||
CHECKOUT_ENVIRONMENT: str(), | ||
PAYPAL_LIVE_ENDPOINT: str(), | ||
PAYPAL_SANDBOX_ENDPOINT: str(), | ||
PAYPAL_CLIENT_ID: str(), | ||
PAYPAL_CLIENT_SECRET: str() | ||
}); | ||
export const envs = cleanEnv( | ||
process.env, | ||
{ | ||
NODE_ENV: str<NodeEnv>({ | ||
devDefault: 'development', | ||
choices: ['development', 'test', 'production'] | ||
}), | ||
JWT_SECRET: str(), | ||
COOKIE_SECRET: str(), | ||
CORS_WHITE_LIST: str(), | ||
MINIO_URL: url(), | ||
MINIO_SERVER_ENDPOINT: host(), | ||
MINIO_PORT: port(), | ||
MINIO_ACCESS_KEY: str(), | ||
MINIO_SECRET_KEY: str(), | ||
MINIO_BUCKET_NAME: str(), | ||
CHECKOUT_ENVIRONMENT: str(), | ||
PAYPAL_LIVE_ENDPOINT: url(), | ||
PAYPAL_SANDBOX_ENDPOINT: url(), | ||
PAYPAL_CLIENT_ID: str(), | ||
PAYPAL_CLIENT_SECRET: str(), | ||
GOOGLE_CLIENT_ID: str({ default: 'anc' }), | ||
GOOGLE_CLIENT_SECRET: str(), | ||
GOOGLE_REDIRECT_URL: url() | ||
}, | ||
{ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
reporter: ({ errors, env }) => { | ||
logger.error(`error of envs: ${errors}`); | ||
for (const [envVar, err] of Object.entries(errors)) { | ||
logger.error(`Invalid env '${envVar}': ${Object.keys(err.message)}`); | ||
} | ||
} | ||
} | ||
); | ||
|
||
export const CORS_WHITE_LIST = envs.CORS_WHITE_LIST.split(','); | ||
export const PAYPAL_ENDPOINT = envs.CHECKOUT_ENVIRONMENT === 'live' ? envs.PAYPAL_LIVE_ENDPOINT : envs.PAYPAL_SANDBOX_ENDPOINT; |
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,8 +1,7 @@ | ||
import { envs } from '@configs'; | ||
|
||
export const cookieOptions = { | ||
signed: false, | ||
secure: envs.isProduction, | ||
//TODO: using envs | ||
secure: process.env.isProduction, | ||
path: '/', | ||
httpOnly: true | ||
}; |
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,9 @@ | ||
import { Static, Type } from '@sinclair/typebox'; | ||
|
||
// See https://github.com/sinclairzx81/typebox | ||
|
||
export const GoogleOAuthParamsDto = Type.Object({ | ||
code: Type.String() | ||
}); | ||
|
||
export type GoogleOAuthParamsDto = Static<typeof GoogleOAuthParamsDto>; |
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,29 @@ | ||
// import { envs } from '@configs'; | ||
import { google } from 'googleapis'; | ||
|
||
export const googleOAuth2Client = new google.auth.OAuth2( | ||
//TODO: using envs | ||
process.env.GOOGLE_CLIENT_ID, | ||
process.env.GOOGLE_CLIENT_SECRET, | ||
process.env.GOOGLE_REDIRECT_URL | ||
); | ||
|
||
export const getUserInfo = async (authorizationCode: string) => { | ||
const { tokens } = await googleOAuth2Client.getToken(authorizationCode); | ||
googleOAuth2Client.setCredentials(tokens); | ||
|
||
const peopleApi = google.people({ version: 'v1', auth: googleOAuth2Client }); | ||
const userInfo = await peopleApi.people.get({ | ||
resourceName: 'people/me', | ||
personFields: 'emailAddresses,names' | ||
}); | ||
|
||
const userEmail = userInfo.data.emailAddresses?.[0].value; | ||
const isVerifiedEmail = userInfo.data.emailAddresses?.[0].metadata?.verified || false; | ||
const userName = userInfo.data.names?.[0].displayName; | ||
return { | ||
userEmail, | ||
userName, | ||
isVerifiedEmail | ||
}; | ||
}; |
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,10 +1,10 @@ | ||
import { envs } from '@configs'; | ||
import * as Minio from 'minio'; | ||
|
||
export const minioClient = new Minio.Client({ | ||
endPoint: envs.MINIO_SERVER_ENDPOINT, | ||
port: envs.MINIO_PORT, | ||
//TODO: using envs | ||
endPoint: process.env.MINIO_SERVER_ENDPOINT || '14.225.192.183', | ||
port: Number(process.env.MINIO_PORT) || 9000, | ||
useSSL: false, | ||
accessKey: envs.MINIO_ACCESS_KEY, | ||
secretKey: envs.MINIO_SECRET_KEY | ||
accessKey: process.env.MINIO_ACCESS_KEY || '', | ||
secretKey: process.env.MINIO_SECRET_KEY || '' | ||
}); |
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,4 +1,5 @@ | ||
import { envs, loggerConfig } from '@configs'; | ||
import { loggerConfig } from '@configs'; | ||
import pino from 'pino'; | ||
|
||
export const logger = pino(loggerConfig[envs.NODE_ENV]); | ||
//TODO: using envs | ||
export const logger = pino(loggerConfig['development']); |
Oops, something went wrong.