-
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.
feat: 通常ユーザーがアクセスできないページにアクセスされたときにリダイレクトするように
- Loading branch information
Showing
7 changed files
with
76 additions
and
23 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
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,24 @@ | ||
import { left, right } from 'fp-ts/lib/Either'; | ||
import type { Either } from 'fp-ts/lib/Either'; | ||
|
||
export type ErrorResponse = | ||
| 'Unauhorization' | ||
| 'Forbidden' | ||
| 'InternalError' | ||
| 'UnknownError'; | ||
|
||
export async function responseJsonOrErrorResponse<T>( | ||
response: Response | ||
): Promise<Either<ErrorResponse, T>> { | ||
if (response.ok) { | ||
return right((await response.json()) as T); | ||
} else if (response.status == 401) { | ||
return left('Unauhorization'); | ||
} else if (response.status == 403) { | ||
return left('Forbidden'); | ||
} else if (response.status == 500) { | ||
return left('InternalError'); | ||
} else { | ||
return left('UnknownError'); | ||
} | ||
} |
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,18 @@ | ||
'use server'; | ||
|
||
import { responseJsonOrErrorResponse } from '@/features/api/responseOrErrorResponse'; | ||
import type { User } from '../types/userSchema'; | ||
|
||
const apiServerUrl = 'http://localhost:9000'; | ||
|
||
export const getUser = async (token: string) => { | ||
const response = await fetch(`${apiServerUrl}/users`, { | ||
method: 'GET', | ||
headers: { | ||
Accept: 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
|
||
return responseJsonOrErrorResponse<User>(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { z } from 'zod'; | ||
|
||
export const userSchema = z.object({ | ||
uuid: z.string().uuid(), | ||
name: z.string(), | ||
role: z.enum(['ADMINISTRATOR', 'STANDARD_USER']), | ||
}); | ||
|
||
export type User = z.infer<typeof userSchema>; |