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.
As the env variables are declaring by yourself, it's kind of meaningless to check if they exist or not. So, you can remove this portion totally:
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")
}
Then to avoid typescript error, you can declare variables in the below given way and the issue will be fixed:
const GITHUB_ID = process.env.GITHUB_ID!;
const GITHUB_SECRET= process.env.GITHUB_SECRET!;
const GOOLE_ID= process.env.GOOGLE_ID!;
const GOOGLE_SECRET= process.env.GOOGLE_SECRET!;
Just copy and paste this code and let me know if you're still facing any issues.