Skip to content

Commit

Permalink
chore: add env with type safe
Browse files Browse the repository at this point in the history
  • Loading branch information
brunosllz committed Aug 26, 2023
1 parent 1ae8fe1 commit 5695476
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 6 deletions.
23 changes: 22 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"@auth/prisma-adapter": "^1.0.1",
"@prisma/client": "^5.2.0",
"@t3-oss/env-nextjs": "^0.6.1",
"axios": "^1.4.0",
"jsonwebtoken": "^9.0.1",
"next": "13.4.13",
Expand Down
11 changes: 6 additions & 5 deletions src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import NextAuth, { NextAuthOptions } from 'next-auth'
import { prisma } from '@/libs/prisma'
import { Adapter } from 'next-auth/adapters'
import { CustomPrismaAdapter } from '@/libs/next-auth/prisma-adapter'
import { env } from '@/env'

export const handler: NextAuthOptions = NextAuth({
adapter: CustomPrismaAdapter(prisma) as Adapter,
providers: [
GitHubProvider({
clientId: process.env.GITHUB_ID ?? '',
clientSecret: process.env.GITHUB_SECRET ?? '',
clientId: env.GITHUB_ID,
clientSecret: env.GITHUB_SECRET,
profile(profile: GithubProfile) {
return {
id: profile.id.toString(),
Expand All @@ -30,8 +31,8 @@ export const handler: NextAuthOptions = NextAuth({
uid: token.sub,
}

const encodedToken = jwt.sign(payload, process.env.JWT_SECRET!, {
algorithm: process.env.JWT_ALGORITHM as jwt.Algorithm,
const encodedToken = jwt.sign(payload, env.JWT_SECRET, {
algorithm: env.JWT_ALGORITHM as jwt.Algorithm,
expiresIn: '24h',
})

Expand All @@ -48,7 +49,7 @@ export const handler: NextAuthOptions = NextAuth({
strategy: 'jwt',
maxAge: 60 * 60 * 24, // 24 hours
},
secret: process.env.NEXTAUTH_SECRET,
secret: env.NEXTAUTH_SECRET,
pages: {
signIn: '/auth/sign-in',
error: '/auth/error',
Expand Down
40 changes: 40 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { createEnv } from '@t3-oss/env-nextjs'
import { z } from 'zod'

const nodeEnv = z.enum(['development', 'production', 'test'])

function requiredOnEnv(env: z.infer<typeof nodeEnv>) {
return (value: any) => {
if (env === process.env.NODE_ENV && !value) {
return false
}

return true
}
}

export const env = createEnv({
server: {
DATABASE_URL: z.string().min(1).url(),
JWT_ALGORITHM: z.string().refine(requiredOnEnv('production')),
JWT_SECRET: z.string().refine(requiredOnEnv('production')),
NEXTAUTH_URL: z.string().optional().refine(requiredOnEnv('production')),
NEXTAUTH_SECRET: z.string().min(1),
GITHUB_ID: z.string().min(1),
GITHUB_SECRET: z.string().min(1),
},
client: {
NEXT_PUBLIC_VERCEL_URL: z.string().min(1).url(),
},
shared: {
NODE_ENV: nodeEnv,
VERCEL_ENV: z
.enum(['production', 'preview', 'development'])
.default('development'),
},
experimental__runtimeEnv: {
NEXT_PUBLIC_VERCEL_URL: process.env.NEXT_PUBLIC_VERCEL_URL,
NODE_ENV: process.env.NODE_ENV,
VERCEL_ENV: process.env.VERCEL_ENV,
},
})
5 changes: 5 additions & 0 deletions src/libs/axios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import axios from 'axios'

export const api = axios.create({
baseURL: process.env.NEXT_PUBLIC_VERCEL_URL,
})

0 comments on commit 5695476

Please sign in to comment.