Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Google Auth provider #19

Merged
merged 3 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ NEXTAUTH_SECRET=mypassword
NEXTAUTH_URL="http://localhost:3000" # dev only
NEXTAUTH_URL_INTERNAL="http://localhost:3000" # dev only

GOOGLE_CLIENT_ID=*****
GOOGLE_CLIENT_SECRET=*****

GITHUB_CLIENT_ID=*****
GITHUB_CLIENT_SECRET=*****
6 changes: 5 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const nextConfig = {
appDir: true,
},
images: {
domains: ["images.unsplash.com", "avatars.githubusercontent.com"],
domains: [
"images.unsplash.com",
"avatars.githubusercontent.com",
"lh3.googleusercontent.com",
],
},
};

Expand Down
6 changes: 2 additions & 4 deletions src/components/SignInButton.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"use client";

import { signIn } from "next-auth/react";
import { ReactNode } from "react";

type LoginProviders = "github"; // | "google"
import { signIn } from "next-auth/react";
import { LoginProviders } from "@/shared/auth";

type Props = {
children: ReactNode;
Expand Down
3 changes: 2 additions & 1 deletion src/components/SignInForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export default function SignInForm(props: Props) {
</div>

<div className="mt-10 sm:mx-auto sm:w-full sm:max-w-sm">
<div className="mt-5">
<div className="mt-5 flex gap-4 flex-col">
<SignInButton provider="google">Continue with Google</SignInButton>
<SignInButton provider="github">Continue with Github</SignInButton>
</div>
{props.error && (
Expand Down
6 changes: 5 additions & 1 deletion src/shared/auth.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { headers } from "next/headers";
import { NextAuthOptions } from "next-auth";
import GithubProvider from "next-auth/providers/github";
import GoogleProvider from "next-auth/providers/google";
import {
NO_SESSION_REDIRECT,
ORIGIN_URL_KEY,
githubCredentials,
googleCredentials,
} from "./settings";

export type LoginProviders = "github" | "google";

export const authOptions: NextAuthOptions = {
providers: [GithubProvider(githubCredentials)],
providers: [GithubProvider(githubCredentials), GoogleProvider(googleCredentials),],
pages: {
signIn: "/login",
},
Expand Down
12 changes: 12 additions & 0 deletions src/shared/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ export const githubCredentials = {
clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
};

export const googleCredentials = {
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
authorization: {
params: {
prompt: "consent",
access_type: "offline",
response_type: "code",
},
},
};

export const ORIGIN_URL_KEY = "x-url";
export const NO_SESSION_REDIRECT = "/login?callbackUrl=";

Expand Down