generated from Medici-Mansion/basic-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SNP-64] Google 로그인 연동
- Loading branch information
Showing
20 changed files
with
580 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ yarn-error.log* | |
|
||
# local env files | ||
.env*.local | ||
.env | ||
|
||
# vercel | ||
.vercel | ||
|
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,63 @@ | ||
import { Session, User } from "@/type"; | ||
import axios from "axios"; | ||
const api = axios.create({ baseURL: process.env.NEXT_PUBLIC_API_URL }); | ||
export const getGoogleCode = () => { | ||
const authUrl = new URL( | ||
"https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount", | ||
); | ||
|
||
authUrl.searchParams.set( | ||
"client_id", | ||
process.env.NEXT_PUBLIC_GOOGLE_AUTH_CLIENT_ID, | ||
); | ||
authUrl.searchParams.set( | ||
"redirect_uri", | ||
process.env.NEXT_PUBLIC_GOOGLE_REDIRECT_URL, | ||
); | ||
authUrl.searchParams.set("response_type", "code"); | ||
authUrl.searchParams.set( | ||
"scope", | ||
"https://www.googleapis.com/auth/userinfo.email", | ||
); | ||
authUrl.searchParams.set("access_type", "offline"); | ||
window.location.href = authUrl.toString(); | ||
}; | ||
|
||
export const getUser = async (code: string) => { | ||
const userResponse = await api.get<Session["user"]>("/oauth/google/user", { | ||
params: { | ||
code, | ||
}, | ||
}); | ||
return userResponse.data; | ||
}; | ||
|
||
export const getNewToken = async (refresh: string) => { | ||
const newToken = await api.post<Session["token"]>("/auth/refresh", { | ||
refresh, | ||
}); | ||
return newToken.data; | ||
}; | ||
|
||
export const getMe = async (access: string) => { | ||
const res = await api.get<User>(`/user/me`, { | ||
headers: { | ||
Authorization: `Bearer ${access}`, | ||
}, | ||
}); | ||
return res.data; | ||
}; | ||
|
||
export const setNickName = async (param: { nickname: string }) => { | ||
const res = await api.post<boolean>("/user/nickname", param); | ||
return res.data; | ||
}; | ||
|
||
const APIs = { | ||
getGoogleCode, | ||
getUser, | ||
getMe, | ||
setNickName, | ||
}; | ||
|
||
export default APIs; |
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,50 @@ | ||
import { Session } from "@/type"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export async function GET( | ||
req: NextRequest, | ||
{ params: { serviceName } }: { params: { serviceName: string } }, | ||
) { | ||
const { searchParams, origin } = new URL(req.url); | ||
|
||
const response = NextResponse.redirect(origin); | ||
try { | ||
const code = searchParams.get("code"); | ||
const scope = searchParams.get("scope"); | ||
|
||
if (code) { | ||
const apiUrl = new URL(process.env.NEXT_PUBLIC_API_URL); | ||
apiUrl.pathname = `/oauth/${serviceName}/user`; | ||
apiUrl.searchParams.set("code", code); | ||
apiUrl.searchParams.set("scope", scope || ""); | ||
const fetcher = await fetch(apiUrl, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
const token = (await fetcher.json()) as Session["token"]; | ||
if (fetcher.status >= 400) { | ||
throw new Error("invalid request - " + JSON.stringify(token)); | ||
} | ||
response.cookies.set({ | ||
name: "access", | ||
value: token?.access || "", | ||
httpOnly: true, | ||
// 하루 - 24시간 | ||
maxAge: 60 * 60 * 24, | ||
}); | ||
response.cookies.set({ | ||
name: "refresh", | ||
value: token?.refresh || "", | ||
httpOnly: true, | ||
// 30일 | ||
maxAge: 60 * 60 * 24 * 30, | ||
}); | ||
} | ||
} catch (error: any) { | ||
console.error(`[AUTH ERROR]: ${error?.message}`); | ||
} | ||
|
||
return response; | ||
} |
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,8 @@ | ||
"use client"; | ||
import React from "react"; | ||
|
||
const NotFoundPage = () => { | ||
return <div>NotFound</div>; | ||
}; | ||
|
||
export default NotFoundPage; |
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,5 @@ | ||
const ErrorPage = () => { | ||
return <div>ErrorPage</div>; | ||
}; | ||
|
||
export default ErrorPage; |
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,5 @@ | ||
const LoadingPage = () => { | ||
return <div>LoadingPage</div>; | ||
}; | ||
|
||
export default LoadingPage; |
Oops, something went wrong.