Skip to content

Commit e50b443

Browse files
authored
add set csrftoken cookie api (#48)
1 parent 4864945 commit e50b443

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Controller } from 'egg';
2+
3+
export default class EmployeeController extends Controller {
4+
public async index() {
5+
const { ctx } = this;
6+
7+
ctx.helper.commonJson(
8+
ctx,
9+
{
10+
data: {}
11+
},
12+
200,
13+
);
14+
}
15+
}

packages/toolkits/pro/template/server/eggJs/app/router.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ export default (app: Application) => {
66
// todo: init database connect
77
// router.post('/v1/database/init');
88

9+
// Must be called before all other interfaces
10+
router.get('/v1/setcsrf', controller.csrf.index);
11+
912
router.post('/v1/employee/getEmployee', controller.employee.getEmployee);
1013

1114
};

packages/toolkits/pro/template/tinyvue/src/api/interceptor.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
22
import { Modal } from '@opentiny/vue';
33
import { getToken } from '@/utils/auth';
44

5+
export function setcsrf() {
6+
return axios.get('/api/v1/setcsrf');
7+
}
8+
59
export interface HttpResponse<T = unknown> {
610
status: number;
711
msg: string;
@@ -22,6 +26,12 @@ axios.interceptors.request.use(
2226
}
2327
config.headers.Authorization = `Bearer ${token}`;
2428
}
29+
30+
const [, csrfToken] = /[;\s+]?csrfToken=([^;]*)/.exec(document.cookie) || [];
31+
if (csrfToken) {
32+
config.headers = { ...config.headers, 'x-csrf-token': csrfToken };
33+
}
34+
2535
return config;
2636
},
2737
(error) => {

0 commit comments

Comments
 (0)