-
Notifications
You must be signed in to change notification settings - Fork 3
Feat/#206 서버사이드 로그인 기능 개발 #207
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
Open
ylem76
wants to merge
8
commits into
main
Choose a base branch
from
feat/#206
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d780738
[#206] add server actions
ylem76 1996a70
[#206] login action 에러 처리
ylem76 9244507
[#206] axios로 변경
ylem76 6eb527b
[#206] middleware 추가
ylem76 4eef97d
[#206] axios server 분리
ylem76 300e0b3
[#206] remove useRedirect
ylem76 fdb24f5
[#206] 미들웨어 수정
ylem76 2a34546
[#206] add user data
ylem76 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,29 @@ | ||
| 'use server' | ||
| import { cookies } from 'next/headers' | ||
| import { redirect } from 'next/navigation' | ||
|
|
||
| import api from '@/lib/axiosServer' | ||
|
|
||
| import { LoginFormValue } from './components/LoginForm' | ||
|
|
||
| export default async function loginAction(data: LoginFormValue) { | ||
| // 백엔드 요청 | ||
| try { | ||
| const response = await api.post('/auth/login', data, {}) | ||
| const { accessToken, user } = response.data | ||
|
|
||
| // 가져온 json token 쿠키 설정 하기 | ||
| cookies().set('Authorization', accessToken, { | ||
| secure: true, | ||
| // httpOnly: true, | ||
| expires: new Date(Date.now() + 24 * 60 * 60 * 1000 * 3), | ||
| // path: '/', | ||
| sameSite: 'strict', | ||
| }) | ||
| } catch (error: any) { | ||
| return error.response?.data?.message || error.message | ||
| } | ||
|
|
||
| // 로그인 여부에 따라 redirect | ||
| redirect('/mydashboard') | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,29 @@ | ||
| 'use server' | ||
|
|
||
| import axios from 'axios' | ||
| import { cookies } from 'next/headers' | ||
|
|
||
| const api = axios.create({ | ||
| baseURL: 'https://sp-taskify-api.vercel.app/7-2', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| withCredentials: true, | ||
| }) | ||
|
|
||
| api.interceptors.request.use( | ||
| config => { | ||
| console.log('interceptor') | ||
| const token = cookies().get('Authorization') | ||
|
|
||
| if (token) { | ||
| config.headers.Authorization = `Bearer ${token}` | ||
| } | ||
| return config | ||
| }, | ||
| error => { | ||
| return Promise.reject(error) | ||
| } | ||
| ) | ||
|
|
||
| export default api |
This file contains hidden or 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,19 @@ | ||
| import { cookies } from 'next/headers' | ||
| import type { NextRequest } from 'next/server' | ||
| import { NextResponse } from 'next/server' | ||
|
|
||
| export async function middleware(request: NextRequest) { | ||
| // 쿠키 확인 | ||
| const cookie = cookies().get('Authorization') | ||
| if (!cookie) { | ||
| return NextResponse.redirect(new URL('/login', request.url)) | ||
| } | ||
|
|
||
| if (cookie && request.nextUrl.pathname === '/') { | ||
| return NextResponse.redirect(new URL('/mydashboard', request.url)) | ||
| } | ||
| } | ||
|
|
||
| export const config = { | ||
| matcher: ['/', '/mydashboard/:path*', '/dashboard/:path*', '/mypage/:path*'], | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
서버 액션 이렇게 적용하는 거군요...!
감사합니다 큰 공부가 되네요