-
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.
* Handle 401 * Update login api * Post with CSRF token * Save csrf token in local storage
- Loading branch information
Showing
9 changed files
with
113 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,49 @@ | ||
const qs = require('qs') | ||
const url = require('url') | ||
const sendJson = require('send-data/json') | ||
|
||
function login(req, res) { | ||
let { username, password } = req.body | ||
if (username === 'admin' && password === '888888') { | ||
sendJson(req, res, { | ||
payload: { | ||
statusCode: 200, | ||
body: { | ||
id: 1, | ||
nickname: 'Jerry', | ||
}, | ||
}) | ||
} else { | ||
res.statusCode = 400 | ||
sendJson(req, res, { | ||
code: 400, | ||
payload: { | ||
message: 'invalid username or password', | ||
statusCode: 400, | ||
body: { | ||
code: 400, | ||
message: 'Invalid username or password', | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
function logout(req, res) { | ||
sendJson(req, res, {}) | ||
} | ||
|
||
function getCurrentUser(req, res) { | ||
sendJson(req, res, { | ||
statusCode: 200, | ||
body: { | ||
id: 1, | ||
nickname: 'Jerry', | ||
}, | ||
}) | ||
} | ||
|
||
function getCsrfToken(req, res) { | ||
sendJson(req, res, { | ||
payload: 'ok', | ||
token: 'mock-token', | ||
}) | ||
} | ||
|
||
module.exports = { | ||
'POST /api/login': login, | ||
'POST /api/logout': logout, | ||
'GET /:controller/:action': (req, res, params) => { | ||
const { query } = url.parse(req.url) | ||
console.log(params, qs.parse(query)) | ||
res.end() | ||
}, | ||
'GET /api/current-user': getCurrentUser, | ||
'GET /api/csrf': getCsrfToken, | ||
} |
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 |
---|---|---|
@@ -1,19 +1,7 @@ | ||
import React from 'react' | ||
import { createBrowserRouter, RouterProvider } from 'react-router-dom' | ||
import Home from './routes/Home' | ||
import Dashboard from './routes/Dashboard' | ||
import Login from './routes/Login' | ||
|
||
const router = createBrowserRouter([ | ||
{ path: '/', element: <Home /> }, | ||
{ path: '/login', element: <Login /> }, | ||
{ path: '/dashboard', element: <Dashboard /> }, | ||
]) | ||
import { RouterProvider } from 'react-router-dom' | ||
import router from './router' | ||
|
||
export default () => { | ||
return ( | ||
<React.StrictMode> | ||
<RouterProvider router={router} /> | ||
</React.StrictMode> | ||
) | ||
return <RouterProvider router={router} /> | ||
} |
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,11 @@ | ||
import React from 'react' | ||
import { createBrowserRouter } from 'react-router-dom' | ||
import Home from './routes/Home' | ||
import Dashboard from './routes/Dashboard' | ||
import Login from './routes/Login' | ||
|
||
export default createBrowserRouter([ | ||
{ path: '/', element: <Home /> }, | ||
{ path: '/login', element: <Login /> }, | ||
{ path: '/dashboard', element: <Dashboard /> }, | ||
]) |
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 |
---|---|---|
@@ -1,19 +1,27 @@ | ||
import { post } from '~/src/common/request' | ||
import { request, post, clearCsrfToken } from '~/src/common/request' | ||
|
||
export interface LoginRequest { | ||
username: string | ||
password: string | ||
} | ||
|
||
export interface LoginResponse { | ||
export interface CurrentUser { | ||
id: number | ||
nickname: string | ||
} | ||
|
||
export async function login(data: LoginRequest): Promise<LoginResponse> { | ||
export async function login(data: LoginRequest): Promise<CurrentUser> { | ||
return post('/api/login', data) | ||
} | ||
|
||
export async function logout() { | ||
return post('/api/logout') | ||
try { | ||
return await post('/api/logout') | ||
} finally { | ||
clearCsrfToken() | ||
} | ||
} | ||
|
||
export async function getCurrentUser(): Promise<CurrentUser> { | ||
return request('/api/current-user') | ||
} |
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