Skip to content

Commit

Permalink
feat: 通常ユーザーがアクセスできないページにアクセスされたときにリダイレクトするように
Browse files Browse the repository at this point in the history
  • Loading branch information
rito528 committed Mar 3, 2024
1 parent 6d7162f commit 0c0795f
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 23 deletions.
9 changes: 9 additions & 0 deletions src/app/admin/forms/create/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { isRight } from 'fp-ts/lib/Either';
import { redirect } from 'next/navigation';
import DashboardMenu from '@/components/DashboardMenu';
import NavBar from '@/components/NavBar';
import { CreateFormComponent } from '@/features/form/components/CreateForm';
import { getCachedToken } from '@/features/user/api/mcToken';
import { getUser } from '@/features/user/api/user';
import styles from '../../../page.module.css';

const Home = async () => {
const token = (await getCachedToken()) ?? '';
const user = await getUser(token);

if (isRight(user) && user.right.role == 'STANDARD_USER') {
redirect('/forbidden');
}

return (
<main className={styles['main']}>
<NavBar />
Expand Down
7 changes: 7 additions & 0 deletions src/app/admin/forms/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isRight } from 'fp-ts/lib/Either';
import { redirect } from 'next/navigation';
import { redirectOrDoNothing } from '@/app/error/RedirectByErrorResponse';
import DashboardMenu from '@/components/DashboardMenu';
import NavBar from '@/components/NavBar';
Expand All @@ -8,11 +9,17 @@ import {
Forms,
} from '@/features/form/components/DashboardFormList';
import { getCachedToken } from '@/features/user/api/mcToken';
import { getUser } from '@/features/user/api/user';
import styles from '../../page.module.css';

const Home = async () => {
const token = (await getCachedToken()) ?? '';
const forms = await getForms(token);
const user = await getUser(token);

if (isRight(user) && user.right.role == 'STANDARD_USER') {
redirect('/forbidden');
}

if (isRight(forms)) {
return (
Expand Down
7 changes: 7 additions & 0 deletions src/app/admin/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { isRight } from 'fp-ts/lib/Either';
import { redirect } from 'next/navigation';
import DataTable from '@/components/Dashboard';
import DashboardMenu from '@/components/DashboardMenu';
import NavBar from '@/components/NavBar';
import { getAllAnswers } from '@/features/form/api/form';
import { getCachedToken } from '@/features/user/api/mcToken';
import { getUser } from '@/features/user/api/user';
import { redirectOrDoNothing } from '../error/RedirectByErrorResponse';
import styles from '../page.module.css';

const Home = async () => {
const token = (await getCachedToken()) ?? '';
const answers = await getAllAnswers(token);
const user = await getUser(token);

if (isRight(user) && user.right.role == 'STANDARD_USER') {
redirect('/forbidden');
}

if (isRight(answers)) {
return (
Expand Down
24 changes: 24 additions & 0 deletions src/features/api/responseOrErrorResponse.ts
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');
}
}
25 changes: 2 additions & 23 deletions src/features/form/api/form.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use server';

import { left, right } from 'fp-ts/lib/Either';
import { responseJsonOrErrorResponse } from '@/features/api/responseOrErrorResponse';
import type {
BatchAnswer,
Form,
FormQuestion,
MinimumForm,
Visibility,
} from '../types/formSchema';
import type { ErrorResponse } from '@/features/api/responseOrErrorResponse';
import type { Either } from 'fp-ts/lib/Either';

const apiServerUrl = 'http://localhost:9000';
Expand Down Expand Up @@ -108,28 +109,6 @@ interface Questions {
isRequired: boolean;
}

export type ErrorResponse =
| 'Unauhorization'
| 'Forbidden'
| 'InternalError'
| 'UnknownError';

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');
}
}

export const createForm = async (
token: string,
formTitle: string,
Expand Down
18 changes: 18 additions & 0 deletions src/features/user/api/user.ts
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);
};
9 changes: 9 additions & 0 deletions src/features/user/types/userSchema.ts
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>;

0 comments on commit 0c0795f

Please sign in to comment.