Skip to content

Commit 4f3732f

Browse files
authored
Merge pull request #25 from Muxi-X/pass.muxi-tech.xyz_auth_fix
Pass.muxi tech.xyz auth fix
2 parents 841752c + 16d111c commit 4f3732f

7 files changed

Lines changed: 117 additions & 46 deletions

File tree

package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,17 @@
4949
},
5050
"devDependencies": {
5151
"react-router-dom": "^5.0.1"
52-
}
52+
},
53+
"description": "This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).",
54+
"main": "index.js",
55+
"repository": {
56+
"type": "git",
57+
"url": "git+https://github.com/Muxi-X/muxi_auth_fe.git"
58+
},
59+
"author": "",
60+
"license": "ISC",
61+
"bugs": {
62+
"url": "https://github.com/Muxi-X/muxi_auth_fe/issues"
63+
},
64+
"homepage": "https://github.com/Muxi-X/muxi_auth_fe#readme"
5365
}

src/common/cookie.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function getCookie(cname) {
33
let ca = document.cookie.split(';');
44
for (var i = 0; i < ca.length; i++) {
55
let c = ca[i].trim();
6-
if (c.indexOf(name) == 0) {
6+
if (c.indexOf(name) === 0) {
77
return c.substring(name.length, c.length);
88
}
99
}

src/common/getFromUrl.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function getQueryVariable(variable) {
2+
let query = window.location.search.substring(1);
3+
let vars = query.split('&');
4+
for (let i = 0; i < vars.length; i++) {
5+
let pair = vars[i].split('=');
6+
if (pair[0] === variable) {
7+
return pair[1];
8+
}
9+
}
10+
return null;
11+
}
12+
export default getQueryVariable;

src/common/service.js

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import 'whatwg-fetch';
22
import Notification from 'rc-notification';
3-
43
function Fetch(url, opt = {}) {
54
opt.method = opt.method || 'GET';
6-
opt.headers = {
7-
Accept: 'application/json',
8-
'Content-Type': 'application/json'
9-
};
5+
opt.headers = {};
106
if (opt.token) {
117
opt.headers.token = opt.token;
128
}
139

1410
opt.body = JSON.stringify(opt.data) || null;
11+
if (opt.formdata) {
12+
opt.body = opt.formdata;
13+
}
1514

1615
return fetch(url, opt)
1716
.then(response => {
@@ -98,6 +97,53 @@ let Service = {
9897
captcha: captcha
9998
}
10099
});
100+
},
101+
// get auth-code
102+
getOauthCode(username, password) {
103+
return Fetch(
104+
'/auth/api/oauth?response_type=code&client_id=51f03389-2a18-4941-ba73-c85d08201d42',
105+
{
106+
method: 'POST',
107+
data: {
108+
username: username,
109+
password: btoa(password)
110+
}
111+
}
112+
);
113+
},
114+
// get auth-token
115+
getOauthToken(aceessCode, client_id, client_secret) {
116+
let formdata = new FormData();
117+
formdata.append('client_secret', client_secret);
118+
formdata.append('code', aceessCode);
119+
//因为要用formdata数据格式,所以暂时去掉headers,不然浏览器会自动将formdata格式转换为WebKitFormBoundary模式
120+
return Fetch(
121+
`/auth/api/oauth/token?response_type=token&grant_type=authorization_code&client_id=${client_id}`,
122+
{
123+
method: 'POST',
124+
formdata: formdata
125+
}
126+
);
127+
},
128+
//refresh-token
129+
refreshtoken(token, client_id, client_secret) {
130+
let formdata = new FormData();
131+
formdata.append('client_secret', client_secret);
132+
formdata.append('refresh_token', token);
133+
return Fetch(
134+
`/auth/api/oauth/token/refresh?grant_type=refresh_token&client_id=${client_id}`,
135+
{
136+
method: 'POST',
137+
formdata: formdata
138+
}
139+
);
140+
},
141+
//get user info
142+
getUserInfo() {
143+
return Fetch(`/auth/api//user`, {
144+
method: 'GET',
145+
token: localStorage.getItem('token')
146+
});
101147
}
102148
};
103149

src/pages/login/login.js

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,26 @@ import { Link } from 'react-router-dom';
33
import Notification from 'rc-notification';
44
import 'rc-notification/assets/index.css';
55
import getCookie from '../../common/cookie';
6-
import { ERROR_CODE } from '../../common/consts';
76
import './login.css';
87
import Service from '../../common/service';
98
import Layout from '../../component/layout';
109
import Button from '../../component/common/button/button';
1110
import Input from '../../component/common/input/input';
12-
import { get } from 'https';
11+
import getFromUrl from '../../common/getFromUrl';
1312

1413
class Login extends Component {
1514
componentDidMount() {
1615
if (localStorage.getItem('checked')) {
17-
Service.Login(
18-
localStorage.getItem('username'),
19-
localStorage.getItem('password')
16+
Service.refreshtoken(
17+
localStorage.getItem('token'),
18+
localStorage.getItem('client_id'),
19+
localStorage.getItem('client_secret')
2020
).then(res => {
21-
if (res !== null && res !== undefined) {
22-
let landing = 'work.muxixyz.com/';
23-
if (landing) {
24-
window.location.href =
25-
'http://' +
26-
landing +
27-
'landing/?username=' +
28-
localStorage.getItem('username') +
29-
'&token=' +
30-
res.token +
31-
'&id=' +
32-
res.user_id;
33-
}
34-
} else {
35-
this.failed = true;
36-
}
21+
console.log(res);
3722
});
3823
}
3924
}
25+
4026
constructor(props) {
4127
super(props);
4228
this.state = {
@@ -72,42 +58,58 @@ class Login extends Component {
7258
});
7359
}
7460
login() {
61+
let client_id = getFromUrl('client_id');
62+
let client_secret = getFromUrl('client_secret');
7563
const { username, password, isChecked } = this.state;
64+
console.log(username, password, isChecked);
7665
if (username && password) {
77-
Service.Login(username, password).then(res => {
78-
if (res.code === ERROR_CODE.USER_NOT_FOUND) {
66+
Service.getOauthCode(username, password).then(res => {
67+
if (res.code === 20102) {
7968
this.alert('用户不存在');
80-
} else if (res.code === ERROR_CODE.PWD_NOT_CORRECT) {
69+
} else if (res.code === 20301) {
8170
this.alert('密码错误');
8271
} else if (res.code === 0) {
83-
this.alert('登录成功');
84-
// 如果登陆成功,缓存登陆信息
72+
this.alert('登录成功,正在跳转');
73+
//如果用户勾选下次自动登录,保存此次登录信息
8574
if (isChecked) {
8675
localStorage.setItem('username', username);
8776
localStorage.setItem('password', password);
8877
localStorage.setItem('checked', isChecked);
8978
}
90-
// landing 逻辑是获取地址栏中的 landing 参数,然后在这个时候跳转。
91-
// landing 参数是应用登陆跳转到内网门户时加在 URL 里面的,比如:http://pass.muxixyz.com/?landing=work.muxixyz.com%2Flanding
92-
// 为了防止内网门户这边路由跳转时 landing 参数丢失,服务端会把 landing 放在cookie里面
93-
// 所以这个从 cookie 里获取 landing 然后跳转就可以
9479

80+
//保存code
81+
let accessCode = res.data.code;
82+
//获取token
83+
Service.getOauthToken(accessCode, client_id, client_secret).then(
84+
res => {
85+
let token = res.data.access_token;
86+
localStorage.setItem('token', token);
87+
localStorage.setItem('client_id', client_id);
88+
localStorage.setItem('client_secret', client_secret);
89+
}
90+
);
91+
//get user info
92+
Service.getUserInfo(localStorage.getItem('token')).then(res => {
93+
console.log(res);
94+
localStorage.setItem('userID', res.data.role_id);
95+
});
96+
//跳转到工作台
9597
let landing = getCookie('landing');
98+
let token = localStorage.getItem('token');
99+
let id = localStorage.getItem('userID');
96100
window.location.href =
97101
'http://' +
98102
landing +
99-
'/?username=' +
103+
'landing/?username=' +
100104
username +
101105
'&token=' +
102-
res.data.token +
106+
token +
103107
'&id=' +
104-
res.data.user_id;
105-
} else {
106-
this.alert('未知错误,请联系应用管理员');
108+
id;
107109
}
108110
});
109111
} else {
110-
this.alert('用户名或密码不能为空');
112+
this.alert('请输入用户名和密码');
111113
}
112114
}
113115

src/pages/register/register.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { Component } from 'react';
22
import './register.css';
33
import { Link } from 'react-router-dom';
4-
import Background from '../../images/login.png';
54
import Service from '../../common/service';
65
import Notification from 'rc-notification';
76
import 'rc-notification/assets/index.css';
@@ -89,6 +88,7 @@ class Register extends Component {
8988
});
9089
if (result === true) {
9190
this.setState({ infoEmail: '该邮箱已被使用!' });
91+
console.log('该邮箱已被使用');
9292
}
9393
});
9494
}
@@ -184,7 +184,7 @@ class Register extends Component {
184184
</div>
185185
<div className="span1">注册</div>
186186
</div>
187-
<form method="post" autocomplete="off">
187+
<form method="post" autoComplete="off">
188188
<div className="input-prepend">
189189
<Input
190190
type="text"

src/pages/reset/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { Component } from 'react';
2-
import Background from '../../images/login.png';
32
import './index.css';
43
import Service from '../../common/service';
54
import Notification from 'rc-notification';

0 commit comments

Comments
 (0)