OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to handle .env.local in a next js typescript file?

  • Thread starter Thread starter Urten
  • Start date Start date
U

Urten

Guest
Code:
import GithubProvider from "next-auth/providers/github"
import GoogleProvider from "next-auth/providers/google"

const GITHUB_ID: string = process.env.GITHUB_ID as string;
const GITHUB_SECRET: string = process.env.GITHUB_SECRET as string;
const GOOLE_ID: string = process.env.GOOGLE_ID as string;
const GOOGLE_SECRET: string = process.env.GOOGLE_SECRET as string;

if (typeof GITHUB_ID !== "string") {
    throw new Error("GITHUB_ID is not a String! check your env file")
}
if (typeof GITHUB_SECRET !== "string") {
    throw new Error("GITHUB_SECRET is not a String, check your env file")
}
if (typeof GOOLE_ID !== "string") {
    throw new Error("GOOLE_ID is not a String, check your env file")
}
if (typeof GOOGLE_SECRET !== "string") {
    throw new Error("GITHUB_ID is not a String, check your env file")
}



export const options = {
    providers: [
        GithubProvider({
            profile(profile) {
                console.log("Profile Github", profile)
                return {
                    ...profile,
                    id: profile.sub, 
                }
            },
            clientId: GITHUB_ID,
            clientSecret: GITHUB_SECRET
        }),
        GoogleProvider({
            profile(profile) {
                console.log("Profile Google", profile)
                return {
                    ...profile,
                    id: profile.sub,
                }
            },
            clientId: GOOLE_ID,
            clientSecret: GOOGLE_SECRET,
        })
    ]
}

I am trying to put environment variables in the cliendID and clientSecret , but before this code, it was showing type error where process.env.GITHUB_ID is 'string | undefined' and cannot be declared to clientID which is a 'string' type. I don't know if this is a proper way to handle this. But is there a better alternative?

I tried other ways to like converting the process.env.GITHUB_ID to string, but it doesn't seem to work.
<pre><code>
import GithubProvider from "next-auth/providers/github"
import GoogleProvider from "next-auth/providers/google"

const GITHUB_ID: string = process.env.GITHUB_ID as string;
const GITHUB_SECRET: string = process.env.GITHUB_SECRET as string;
const GOOLE_ID: string = process.env.GOOGLE_ID as string;
const GOOGLE_SECRET: string = process.env.GOOGLE_SECRET as string;

if (typeof GITHUB_ID !== "string") {
throw new Error("GITHUB_ID is not a String! check your env file")
}
if (typeof GITHUB_SECRET !== "string") {
throw new Error("GITHUB_SECRET is not a String, check your env file")
}
if (typeof GOOLE_ID !== "string") {
throw new Error("GOOLE_ID is not a String, check your env file")
}
if (typeof GOOGLE_SECRET !== "string") {
throw new Error("GITHUB_ID is not a String, check your env file")
}



export const options = {
providers: [
GithubProvider({
profile(profile) {
console.log("Profile Github", profile)
return {
...profile,
id: profile.sub,
}
},
clientId: GITHUB_ID,
clientSecret: GITHUB_SECRET
}),
GoogleProvider({
profile(profile) {
console.log("Profile Google", profile)
return {
...profile,
id: profile.sub,
}
},
clientId: GOOLE_ID,
clientSecret: GOOGLE_SECRET,
})
]
}

</code></pre>
<p>I am trying to put environment variables in the <code>cliendID</code> and <code>clientSecret </code>, but before this code, it was showing type error where <code>process.env.GITHUB_ID is 'string | undefined' and cannot be declared to clientID which is a 'string' type</code>. I don't know if this is a proper way to handle this. But is there a better alternative?</p>
<p>I tried other ways to like converting the process.env.GITHUB_ID to string, but it doesn't seem to work.</p>
Continue reading...
 

Latest posts

P
Replies
0
Views
1
Paras Chouhan
P
Top