Check user auth in api request
This commit is contained in:
@@ -1,21 +1,82 @@
|
|||||||
const jwt = require('jsonwebtoken');
|
const jwt = require('jsonwebtoken');
|
||||||
const { defaultDangerKey } = require('../routes/auth/authSettings');
|
const { defaultDangerKey } = require('../routes/auth/authSettings');
|
||||||
|
const defineProject = require('../models/projects');
|
||||||
|
const { DataTypes } = require('sequelize');
|
||||||
|
|
||||||
function verifySinedIn(req, res, next) {
|
function authMiddleware(sequelize) {
|
||||||
const token = req.header('Authorization');
|
/**
|
||||||
const secretKey = process.env.SECRET_KEY || defaultDangerKey;
|
* Verify user sined in
|
||||||
|
*
|
||||||
|
* If verification is successful, set userId in req.userId.
|
||||||
|
*/
|
||||||
|
function verifySignedIn(req, res, next) {
|
||||||
|
const authHeader = req.header('Authorization');
|
||||||
|
const secretKey = process.env.SECRET_KEY || defaultDangerKey;
|
||||||
|
|
||||||
if (!token) {
|
const token = authHeader.split(' ')[1]; // delete 'Bearer '
|
||||||
return res.status(401).json({ error: 'Access denied' });
|
if (!token) {
|
||||||
|
return res.status(401).json({ error: 'Access denied' });
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const decoded = jwt.verify(token, secretKey);
|
||||||
|
req.userId = decoded.userId;
|
||||||
|
next();
|
||||||
|
} catch (error) {
|
||||||
|
res.status(401).json({ error: 'Invalid token' });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
/**
|
||||||
const decoded = jwt.verify(token, secretKey);
|
* Verify user can access project
|
||||||
req.userId = decoded.userId;
|
* (have to be called after verifySignedIn() middleware)
|
||||||
|
*/
|
||||||
|
async function verifyProjectVisible(req, res, next) {
|
||||||
|
const Project = defineProject(sequelize, DataTypes);
|
||||||
|
|
||||||
|
const projectId = req.params.projectId;
|
||||||
|
if (!projectId) {
|
||||||
|
return res.status(400).json({ error: 'projectId is required' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const project = await Project.findByPk(projectId);
|
||||||
|
if (!project) {
|
||||||
|
return res.status(404).send('Project not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
// if project is private, only project owner can access
|
||||||
|
if (!project.isPublic && project.userId !== req.userId) {
|
||||||
|
return res.status(403).json({ error: 'Forbidden' });
|
||||||
|
}
|
||||||
|
|
||||||
next();
|
next();
|
||||||
} catch (error) {
|
|
||||||
res.status(401).json({ error: 'Invalid token' });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify user has project
|
||||||
|
* (have to be called after verifySignedIn() middleware)
|
||||||
|
*/
|
||||||
|
async function verifyProjectOwner(req, res, next) {
|
||||||
|
const Project = defineProject(sequelize, DataTypes);
|
||||||
|
|
||||||
|
const projectId = req.params.projectId;
|
||||||
|
if (!projectId) {
|
||||||
|
return res.status(400).json({ error: 'projectId is required' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const project = await Project.findByPk(projectId);
|
||||||
|
if (!project) {
|
||||||
|
return res.status(404).send('Project not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (project.userId !== req.userId) {
|
||||||
|
return res.status(403).json({ error: 'Forbidden' });
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
|
||||||
|
return { verifySignedIn, verifyProjectVisible, verifyProjectOwner };
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { verifySinedIn };
|
module.exports = authMiddleware;
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ const defineRunCase = require('../../models/runCases');
|
|||||||
const { DataTypes } = require('sequelize');
|
const { DataTypes } = require('sequelize');
|
||||||
|
|
||||||
module.exports = function (sequelize) {
|
module.exports = function (sequelize) {
|
||||||
|
const { verifySignedIn, verifyProjectVisible } = require('../../middleware/auth')(sequelize);
|
||||||
|
|
||||||
const Project = defineProject(sequelize, DataTypes);
|
const Project = defineProject(sequelize, DataTypes);
|
||||||
const Folder = defineFolder(sequelize, DataTypes);
|
const Folder = defineFolder(sequelize, DataTypes);
|
||||||
const Case = defineCase(sequelize, DataTypes);
|
const Case = defineCase(sequelize, DataTypes);
|
||||||
@@ -18,7 +20,7 @@ module.exports = function (sequelize) {
|
|||||||
Project.hasMany(Run, { foreignKey: 'projectId' });
|
Project.hasMany(Run, { foreignKey: 'projectId' });
|
||||||
Run.hasMany(RunCase, { foreignKey: 'runId' });
|
Run.hasMany(RunCase, { foreignKey: 'runId' });
|
||||||
|
|
||||||
router.get('/:projectId', async (req, res) => {
|
router.get('/:projectId', verifySignedIn, verifyProjectVisible, async (req, res) => {
|
||||||
const projectId = req.params.projectId;
|
const projectId = req.params.projectId;
|
||||||
|
|
||||||
if (!projectId) {
|
if (!projectId) {
|
||||||
|
|||||||
@@ -4,9 +4,10 @@ const defineProject = require('../../models/projects');
|
|||||||
const { DataTypes } = require('sequelize');
|
const { DataTypes } = require('sequelize');
|
||||||
|
|
||||||
module.exports = function (sequelize) {
|
module.exports = function (sequelize) {
|
||||||
|
const { verifySignedIn, verifyProjectOwner } = require('../../middleware/auth')(sequelize);
|
||||||
const Project = defineProject(sequelize, DataTypes);
|
const Project = defineProject(sequelize, DataTypes);
|
||||||
|
|
||||||
router.delete('/:projectId', async (req, res) => {
|
router.delete('/:projectId', verifySignedIn, verifyProjectOwner, async (req, res) => {
|
||||||
const projectId = req.params.projectId;
|
const projectId = req.params.projectId;
|
||||||
try {
|
try {
|
||||||
const project = await Project.findByPk(projectId);
|
const project = await Project.findByPk(projectId);
|
||||||
|
|||||||
@@ -4,9 +4,10 @@ const defineProject = require('../../models/projects');
|
|||||||
const { DataTypes } = require('sequelize');
|
const { DataTypes } = require('sequelize');
|
||||||
|
|
||||||
module.exports = function (sequelize) {
|
module.exports = function (sequelize) {
|
||||||
|
const { verifySignedIn, verifyProjectOwner } = require('../../middleware/auth')(sequelize);
|
||||||
const Project = defineProject(sequelize, DataTypes);
|
const Project = defineProject(sequelize, DataTypes);
|
||||||
|
|
||||||
router.put('/:projectId', async (req, res) => {
|
router.put('/:projectId', verifySignedIn, verifyProjectOwner, async (req, res) => {
|
||||||
const projectId = req.params.projectId;
|
const projectId = req.params.projectId;
|
||||||
const { name, detail, isPublic } = req.body;
|
const { name, detail, isPublic } = req.body;
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,15 +1,19 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const defineProject = require('../../models/projects');
|
const defineProject = require('../../models/projects');
|
||||||
const { DataTypes } = require('sequelize');
|
const { DataTypes, Op } = require('sequelize');
|
||||||
const { verifySinedIn } = require('../../middleware/auth');
|
|
||||||
|
|
||||||
module.exports = function (sequelize) {
|
module.exports = function (sequelize) {
|
||||||
|
const { verifySignedIn } = require('../../middleware/auth')(sequelize);
|
||||||
const Project = defineProject(sequelize, DataTypes);
|
const Project = defineProject(sequelize, DataTypes);
|
||||||
|
|
||||||
router.get('/', verifySinedIn, async (req, res) => {
|
router.get('/', verifySignedIn, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const projects = await Project.findAll();
|
const projects = await Project.findAll({
|
||||||
|
where: {
|
||||||
|
[Op.or]: [{ isPublic: true }, { userId: req.userId }],
|
||||||
|
},
|
||||||
|
});
|
||||||
res.json(projects);
|
res.json(projects);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|||||||
@@ -4,16 +4,17 @@ const defineProject = require('../../models/projects');
|
|||||||
const { DataTypes } = require('sequelize');
|
const { DataTypes } = require('sequelize');
|
||||||
|
|
||||||
module.exports = function (sequelize) {
|
module.exports = function (sequelize) {
|
||||||
|
const { verifySignedIn } = require('../../middleware/auth')(sequelize);
|
||||||
const Project = defineProject(sequelize, DataTypes);
|
const Project = defineProject(sequelize, DataTypes);
|
||||||
|
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', verifySignedIn, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { name, detail, isPublic, userId } = req.body;
|
const { name, detail, isPublic } = req.body;
|
||||||
const newProject = await Project.create({
|
const newProject = await Project.create({
|
||||||
name,
|
name,
|
||||||
detail,
|
detail,
|
||||||
isPublic,
|
isPublic,
|
||||||
userId,
|
userId: req.userId,
|
||||||
});
|
});
|
||||||
res.json(newProject);
|
res.json(newProject);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -3,25 +3,19 @@ const router = express.Router();
|
|||||||
const defineProject = require('../../models/projects');
|
const defineProject = require('../../models/projects');
|
||||||
const defineFolder = require('../../models/folders');
|
const defineFolder = require('../../models/folders');
|
||||||
const { DataTypes } = require('sequelize');
|
const { DataTypes } = require('sequelize');
|
||||||
const { verifySinedIn } = require('../../middleware/auth');
|
|
||||||
|
|
||||||
module.exports = function (sequelize) {
|
module.exports = function (sequelize) {
|
||||||
|
const { verifySignedIn, verifyProjectVisible } = require('../../middleware/auth')(sequelize);
|
||||||
const Project = defineProject(sequelize, DataTypes);
|
const Project = defineProject(sequelize, DataTypes);
|
||||||
const Folder = defineFolder(sequelize, DataTypes);
|
const Folder = defineFolder(sequelize, DataTypes);
|
||||||
Project.hasMany(Folder, { foreignKey: 'projectId' });
|
Project.hasMany(Folder, { foreignKey: 'projectId' });
|
||||||
|
|
||||||
router.get('/:projectId', verifySinedIn, async (req, res) => {
|
router.get('/:projectId', verifySignedIn, verifyProjectVisible, async (req, res) => {
|
||||||
const projectId = req.params.projectId;
|
const projectId = req.params.projectId;
|
||||||
|
|
||||||
if (!projectId) {
|
if (!projectId) {
|
||||||
return res.status(400).json({ error: 'projectId is required' });
|
return res.status(400).json({ error: 'projectId is required' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// if project is private, only project owner can access
|
|
||||||
if (!project.isPublic && project.userId !== req.userId) {
|
|
||||||
return res.status(403).json({ error: 'Forbidden' });
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const project = await Project.findByPk(projectId, {
|
const project = await Project.findByPk(projectId, {
|
||||||
include: [
|
include: [
|
||||||
|
|||||||
@@ -65,6 +65,7 @@
|
|||||||
"project_name": "Project Name",
|
"project_name": "Project Name",
|
||||||
"project_detail": "Project Detail",
|
"project_detail": "Project Detail",
|
||||||
"public": "Public",
|
"public": "Public",
|
||||||
|
"private": "Private",
|
||||||
"if_you_make_public": "If you make project public, everyone who has access to this site will be able to view it.",
|
"if_you_make_public": "If you make project public, everyone who has access to this site will be able to view it.",
|
||||||
"close": "Close",
|
"close": "Close",
|
||||||
"create": "Create",
|
"create": "Create",
|
||||||
|
|||||||
@@ -64,6 +64,7 @@
|
|||||||
"project_name": "プロジェクト名",
|
"project_name": "プロジェクト名",
|
||||||
"project_detail": "プロジェクト詳細",
|
"project_detail": "プロジェクト詳細",
|
||||||
"public": "パブリック",
|
"public": "パブリック",
|
||||||
|
"private": "プライベート",
|
||||||
"if_you_make_public": "プロジェクトをパブリックにすると、このサイトにアクセスできるすべてのユーザーがプロジェクトを見ることができます。",
|
"if_you_make_public": "プロジェクトをパブリックにすると、このサイトにアクセスできるすべてのユーザーがプロジェクトを見ることができます。",
|
||||||
"close": "閉じる",
|
"close": "閉じる",
|
||||||
"create": "作成",
|
"create": "作成",
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import { TokenContextType, TokenType } from '@/types/user';
|
|||||||
import { TokenProps } from '@/types/user';
|
import { TokenProps } from '@/types/user';
|
||||||
import { useRouter, usePathname } from '@/src/navigation';
|
import { useRouter, usePathname } from '@/src/navigation';
|
||||||
const LOCAL_STORAGE_KEY = 'testplat-auth-token';
|
const LOCAL_STORAGE_KEY = 'testplat-auth-token';
|
||||||
const privatePaths = ['/account', '/projects'];
|
|
||||||
|
|
||||||
function storeTokenToLocalStorage(token: TokenType) {
|
function storeTokenToLocalStorage(token: TokenType) {
|
||||||
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(token));
|
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(token));
|
||||||
@@ -43,6 +42,8 @@ const TokenProvider = ({ locale, children }: TokenProps) => {
|
|||||||
// check expire date
|
// check expire date
|
||||||
if (Date.now() < token.expires_at) {
|
if (Date.now() < token.expires_at) {
|
||||||
return true;
|
return true;
|
||||||
|
} else {
|
||||||
|
console.error('session expired');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,17 +72,16 @@ const TokenProvider = ({ locale, children }: TokenProps) => {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const isPrivatePath = () => {
|
// check current path is private. pravate path is '/account/*' or '/projects/*'
|
||||||
return privatePaths.some((path) => {
|
const isPrivatePath = (pathname: string) => {
|
||||||
return path === pathname;
|
return /^\/(account|projects)\/.*/.test(pathname);
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const checkSignInPage = () => {
|
const checkSignInPage = () => {
|
||||||
if (!hasRestoreFinished) {
|
if (!hasRestoreFinished) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (isPrivatePath() && !isSignedIn()) {
|
if (isPrivatePath(pathname) && !isSignedIn()) {
|
||||||
router.push(`/account/signin`, { locale: locale });
|
router.push(`/account/signin`, { locale: locale });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -48,11 +48,11 @@ export default function ProjectsPage({ messages, locale }: Props) {
|
|||||||
|
|
||||||
const onSubmit = async (name: string, detail: string, isPublic: boolean) => {
|
const onSubmit = async (name: string, detail: string, isPublic: boolean) => {
|
||||||
if (editingProject) {
|
if (editingProject) {
|
||||||
const updatedProject = await updateProject(editingProject.id, name, detail, isPublic);
|
const updatedProject = await updateProject(context.token.access_token, editingProject.id, name, detail, isPublic);
|
||||||
const updatedProjects = projects.map((project) => (project.id === updatedProject.id ? updatedProject : project));
|
const updatedProjects = projects.map((project) => (project.id === updatedProject.id ? updatedProject : project));
|
||||||
setProjects(updatedProjects);
|
setProjects(updatedProjects);
|
||||||
} else {
|
} else {
|
||||||
const newProject = await createProject(name, detail, isPublic, context.token.user.id);
|
const newProject = await createProject(context.token.access_token, name, detail, isPublic);
|
||||||
setProjects([...projects, newProject]);
|
setProjects([...projects, newProject]);
|
||||||
}
|
}
|
||||||
closeDialog();
|
closeDialog();
|
||||||
@@ -64,12 +64,11 @@ export default function ProjectsPage({ messages, locale }: Props) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const onDeleteClick = async (projectId: number) => {
|
const onDeleteClick = async (projectId: number) => {
|
||||||
try {
|
// TODO cannot refer context
|
||||||
await deleteProject(projectId);
|
console.log(context);
|
||||||
setProjects(projects.filter((project) => project.id !== projectId));
|
console.log(context.token.access_token);
|
||||||
} catch (error: any) {
|
await deleteProject(context.token.access_token, projectId);
|
||||||
console.error('Error deleting project:', error);
|
setProjects(projects.filter((project) => project.id !== projectId));
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -62,7 +62,15 @@ export default function ProjectsTable({ projects, onEditProject, onDeleteProject
|
|||||||
case 'id':
|
case 'id':
|
||||||
return <span>{cellValue}</span>;
|
return <span>{cellValue}</span>;
|
||||||
case 'isPublic':
|
case 'isPublic':
|
||||||
return cellValue ? <Chip size="sm">{messages.public}</Chip> : <></>;
|
return cellValue ? (
|
||||||
|
<Chip size="sm" variant="bordered">
|
||||||
|
{messages.public}
|
||||||
|
</Chip>
|
||||||
|
) : (
|
||||||
|
<Chip size="sm" variant="bordered">
|
||||||
|
{messages.private}
|
||||||
|
</Chip>
|
||||||
|
);
|
||||||
case 'name':
|
case 'name':
|
||||||
const maxLength = 30;
|
const maxLength = 30;
|
||||||
const truncatedDetail = truncateText(project.detail, maxLength);
|
const truncatedDetail = truncateText(project.detail, maxLength);
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
'use client';
|
'use client';
|
||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect, useContext } from 'react';
|
||||||
import { Divider } from '@nextui-org/react';
|
|
||||||
import { title, subtitle } from '@/components/primitives';
|
import { title, subtitle } from '@/components/primitives';
|
||||||
import { Card, CardBody, Chip } from '@nextui-org/react';
|
import { Card, CardBody, Chip, Divider } from '@nextui-org/react';
|
||||||
import { Folder, Clipboard, FlaskConical } from 'lucide-react';
|
import { Folder, Clipboard, FlaskConical } from 'lucide-react';
|
||||||
import { CaseTypeCountType, CasePriorityCountType } from '@/types/case';
|
import { CaseTypeCountType, CasePriorityCountType } from '@/types/case';
|
||||||
import { ProgressSeriesType } from '@/types/run';
|
import { ProgressSeriesType } from '@/types/run';
|
||||||
import { HomeMessages } from './page';
|
import { HomeMessages } from './page';
|
||||||
|
import { TokenContext } from '@/src/app/[locale]/TokenProvider';
|
||||||
import { aggregateBasicInfo, aggregateTestPriority, aggregateTestType, aggregateProgress } from './aggregate';
|
import { aggregateBasicInfo, aggregateTestPriority, aggregateTestType, aggregateProgress } from './aggregate';
|
||||||
import TestTypesChart from './TestTypesDonutChart';
|
import TestTypesChart from './TestTypesDonutChart';
|
||||||
import TestPriorityChart from './TestPriorityDonutChart';
|
import TestPriorityChart from './TestPriorityDonutChart';
|
||||||
@@ -15,19 +15,22 @@ import Config from '@/config/config';
|
|||||||
import { useTheme } from 'next-themes';
|
import { useTheme } from 'next-themes';
|
||||||
const apiServer = Config.apiServer;
|
const apiServer = Config.apiServer;
|
||||||
|
|
||||||
async function fetchProject(url) {
|
async function fetchProject(jwt: string, projectId: number) {
|
||||||
try {
|
const fetchOptions = {
|
||||||
const response = await fetch(url, {
|
method: 'GET',
|
||||||
method: 'GET',
|
headers: {
|
||||||
headers: {
|
'Content-Type': 'application/json',
|
||||||
'Content-Type': 'application/json',
|
Authorization: jwt,
|
||||||
},
|
},
|
||||||
});
|
};
|
||||||
|
|
||||||
|
const url = `${apiServer}/home/${projectId}`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(url, fetchOptions);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
return data;
|
return data;
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
@@ -41,6 +44,7 @@ type Props = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export function Home({ projectId, messages }: Props) {
|
export function Home({ projectId, messages }: Props) {
|
||||||
|
const context = useContext(TokenContext);
|
||||||
const { theme, setTheme } = useTheme();
|
const { theme, setTheme } = useTheme();
|
||||||
const [project, setProject] = useState({
|
const [project, setProject] = useState({
|
||||||
name: '',
|
name: '',
|
||||||
@@ -55,12 +59,15 @@ export function Home({ projectId, messages }: Props) {
|
|||||||
const [priorityCounts, setPriorityCounts] = useState<CasePriorityCountType[]>();
|
const [priorityCounts, setPriorityCounts] = useState<CasePriorityCountType[]>();
|
||||||
const [progressCategories, setProgressCategories] = useState<string[]>();
|
const [progressCategories, setProgressCategories] = useState<string[]>();
|
||||||
const [progressSeries, setProgressSeries] = useState<ProgressSeriesType[]>();
|
const [progressSeries, setProgressSeries] = useState<ProgressSeriesType[]>();
|
||||||
const url = `${apiServer}/home/${projectId}`;
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function fetchDataEffect() {
|
async function fetchDataEffect() {
|
||||||
|
if (!context.isSignedIn()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = await fetchProject(url);
|
const data = await fetchProject(context.token.access_token, projectId);
|
||||||
setProject(data);
|
setProject(data);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('Error in effect:', error.message);
|
console.error('Error in effect:', error.message);
|
||||||
@@ -68,7 +75,7 @@ export function Home({ projectId, messages }: Props) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fetchDataEffect();
|
fetchDataEffect();
|
||||||
}, [url]);
|
}, [context]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function aggregate() {
|
async function aggregate() {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ export default function Page(params: { locale: string }) {
|
|||||||
projectName: t('project_name'),
|
projectName: t('project_name'),
|
||||||
projectDetail: t('project_detail'),
|
projectDetail: t('project_detail'),
|
||||||
public: t('public'),
|
public: t('public'),
|
||||||
|
private: t('private'),
|
||||||
ifYouMakePublic: t('if_you_make_public'),
|
ifYouMakePublic: t('if_you_make_public'),
|
||||||
close: t('close'),
|
close: t('close'),
|
||||||
create: t('create'),
|
create: t('create'),
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ async function fetchProjects(jwt: string) {
|
|||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
Authorization: jwt,
|
Authorization: `Bearer ${jwt}`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -30,18 +30,18 @@ async function fetchProjects(jwt: string) {
|
|||||||
/**
|
/**
|
||||||
* Create project
|
* Create project
|
||||||
*/
|
*/
|
||||||
async function createProject(name: string, detail: string, isPublic: boolean, userId: number) {
|
async function createProject(jwt: string, name: string, detail: string, isPublic: boolean) {
|
||||||
const newProjectData = {
|
const newProjectData = {
|
||||||
name,
|
name,
|
||||||
detail,
|
detail,
|
||||||
isPublic,
|
isPublic,
|
||||||
userId,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchOptions = {
|
const fetchOptions = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: `Bearer ${jwt}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify(newProjectData),
|
body: JSON.stringify(newProjectData),
|
||||||
};
|
};
|
||||||
@@ -64,7 +64,7 @@ async function createProject(name: string, detail: string, isPublic: boolean, us
|
|||||||
/**
|
/**
|
||||||
* Update project
|
* Update project
|
||||||
*/
|
*/
|
||||||
async function updateProject(projectId: number, name: string, detail: string, isPublic: boolean) {
|
async function updateProject(jwt: string, projectId: number, name: string, detail: string, isPublic: boolean) {
|
||||||
const updatedProjectData = {
|
const updatedProjectData = {
|
||||||
name,
|
name,
|
||||||
detail,
|
detail,
|
||||||
@@ -75,6 +75,7 @@ async function updateProject(projectId: number, name: string, detail: string, is
|
|||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: `Bearer ${jwt}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify(updatedProjectData),
|
body: JSON.stringify(updatedProjectData),
|
||||||
};
|
};
|
||||||
@@ -97,13 +98,15 @@ async function updateProject(projectId: number, name: string, detail: string, is
|
|||||||
/**
|
/**
|
||||||
* Delete project
|
* Delete project
|
||||||
*/
|
*/
|
||||||
async function deleteProject(projectId: number) {
|
async function deleteProject(jwt: string, projectId: number) {
|
||||||
const fetchOptions = {
|
const fetchOptions = {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: `Bearer ${jwt}`,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
console.log(jwt);
|
||||||
|
|
||||||
const url = `${apiServer}/projects/${projectId}`;
|
const url = `${apiServer}/projects/${projectId}`;
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ export type ProjectsMessages = {
|
|||||||
projectName: string;
|
projectName: string;
|
||||||
projectDetail: string;
|
projectDetail: string;
|
||||||
public: string;
|
public: string;
|
||||||
|
private: string;
|
||||||
ifYouMakePublic: string;
|
ifYouMakePublic: string;
|
||||||
close: string;
|
close: string;
|
||||||
create: string;
|
create: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user