From 9cd10e227a54d757da03cfd59cccf9774a531523 Mon Sep 17 00:00:00 2001 From: Hongyuan Ma Date: Tue, 3 Jul 2018 21:21:54 +0800 Subject: [PATCH] Debug login function;Add protal page --- front-end/src/page/login/index.jsx | 70 +++++++++++++++++++-- front-end/src/page/portal/index.css | 0 front-end/src/page/portal/index.jsx | 39 ++++++++++++ front-end/src/service/user-service.jsx | 50 +++++++++++++++ web/apps/users/views.py | 18 ++++++ web/extra_apps/auth/__init__.py | 0 web/{pgperffarm => extra_apps/auth}/auth.py | 3 +- web/pgperffarm/settings.py | 11 ++-- web/pgperffarm/urls.py | 8 +++ 9 files changed, 185 insertions(+), 14 deletions(-) create mode 100644 front-end/src/page/portal/index.css create mode 100644 front-end/src/page/portal/index.jsx create mode 100644 front-end/src/service/user-service.jsx create mode 100644 web/extra_apps/auth/__init__.py rename web/{pgperffarm => extra_apps/auth}/auth.py (98%) diff --git a/front-end/src/page/login/index.jsx b/front-end/src/page/login/index.jsx index 705ebf0..5e29a7c 100644 --- a/front-end/src/page/login/index.jsx +++ b/front-end/src/page/login/index.jsx @@ -1,10 +1,63 @@ import React from 'react'; // import './index.css'; +import PGUtil from 'util/util.jsx' +const _util = new PGUtil(); +import User from 'service/user-service.jsx' +const _user = new User(); -class Login extends React.Component{ - render(){ +class Login extends React.Component { + constructor(props) { + super(props); + this.state = { + username: '', + password: '', + redirect: 'portal' + } + } + + componentWillMount() { + document.title = 'LOGIN'; + } + + onInputChange(e) { + let inputValue = e.target.value, + inputName = e.target.name; + this.setState({ + [inputName]: inputValue + }); + } + + onInputKeyUp(e) { + if (e.keyCode === 13) { + this.onSubmit(); + } + } + + onSubmit() { + let loginInfo = { + username: this.state.username, + password: this.state.password + }, + checkResult = _user.checkLoginInfo(loginInfo); + // check success + if (checkResult.status) { + _user.login(loginInfo).then((res) => { + _util.setStorage('userInfo', res); + this.props.history.push(this.state.redirect); + }, (errMsg) => { + _util.errorTips(errMsg); + }); + } + // check failure + else { + _util.errorTips(checkResult.msg); + } + + } + + render() { return ( -
+
@@ -19,12 +72,17 @@ class Login extends React.Component{
{/**/} - + this.onInputKeyUp(e)} + onChange={e => this.onInputChange(e)}/>
- + this.onInputKeyUp(e)} + onChange={e => this.onInputChange(e)}/>
- +
{/**/} diff --git a/front-end/src/page/portal/index.css b/front-end/src/page/portal/index.css new file mode 100644 index 0000000..e69de29 diff --git a/front-end/src/page/portal/index.jsx b/front-end/src/page/portal/index.jsx new file mode 100644 index 0000000..c20f25b --- /dev/null +++ b/front-end/src/page/portal/index.jsx @@ -0,0 +1,39 @@ +import React from 'react'; +// import './index.css'; +import ResultFilter from 'component/result-filter/index.jsx'; +import BasicTable from 'util/basic-table/index.jsx'; +import Record from 'service/record-service.jsx' +import PGUtil from 'util/util.jsx' + +const _util = new PGUtil(); +const _record = new Record(); +class Portal extends React.Component { + constructor(props) { + super(props); + this.state = { + isLoading: false, + } + + } + + render() { + let show = this.state.isLoading ? "none" : "block"; + let style = { + display: show + }; + + return ( +
+

portal page

+

+ +

+ + {/**/} + +
+ ) + } +} + +export default Portal; \ No newline at end of file diff --git a/front-end/src/service/user-service.jsx b/front-end/src/service/user-service.jsx new file mode 100644 index 0000000..9668a5f --- /dev/null +++ b/front-end/src/service/user-service.jsx @@ -0,0 +1,50 @@ +import PGUtil from 'util/util.jsx' +import PGConstant from 'util/constant.jsx' +const _util = new PGUtil(); + +class User{ + login(loginInfo){ + let url = PGConstant.base_url + '/login'; + return _util.request({ + type: 'post', + url: url, + data: loginInfo + }); + } + // check if the loginInfo is legel + checkLoginInfo(loginInfo){ + let username = $.trim(loginInfo.username), + password = $.trim(loginInfo.password); + // check username + if(typeof username !== 'string' || username.length ===0){ + return { + status: false, + msg: 'username cannot be an empty string!' + } + } + // check password + if(typeof password !== 'string' || password.length ===0){ + return { + status: false, + msg: 'password cannot be an empty string!!' + } + } + return { + status : true, + msg : 'justify pass' + } + } + // logout + logout(){ + let url = PGConstant.base_url + '/logout'; + return _util.request({ + type : 'post', + url : url + }); + } + // getUserList(pageNum){ + // # todo + // } +} + +export default User; \ No newline at end of file diff --git a/web/apps/users/views.py b/web/apps/users/views.py index e784a0b..19668cc 100644 --- a/web/apps/users/views.py +++ b/web/apps/users/views.py @@ -1,6 +1,24 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals +from django.contrib.auth.backends import ModelBackend +from django.db.models import Q from django.shortcuts import render # Create your views here. +from models import UserProfile + + +class CustomBackend(ModelBackend): + """ + custom user auth + """ + def authenticate(self, username=None, password=None, **kwargs): + try: + user = UserProfile.objects.get(Q(username=username)) + if user.check_password(password): + return user + + + except Exception as e: + return None \ No newline at end of file diff --git a/web/extra_apps/auth/__init__.py b/web/extra_apps/auth/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/web/pgperffarm/auth.py b/web/extra_apps/auth/auth.py similarity index 98% rename from web/pgperffarm/auth.py rename to web/extra_apps/auth/auth.py index b1d74b7..c60728e 100644 --- a/web/pgperffarm/auth.py +++ b/web/extra_apps/auth/auth.py @@ -19,7 +19,8 @@ # from django.http import HttpResponse, HttpResponseRedirect -from django.contrib.auth.models import User +# from django.contrib.auth.models import User +from users.models import UserProfile as User from django.contrib.auth.backends import ModelBackend from django.contrib.auth import login as django_login from django.contrib.auth import logout as django_logout diff --git a/web/pgperffarm/settings.py b/web/pgperffarm/settings.py index fdb650d..8f6d381 100644 --- a/web/pgperffarm/settings.py +++ b/web/pgperffarm/settings.py @@ -46,8 +46,8 @@ INSTALLED_APPS = ( 'django.contrib.staticfiles', 'django_gravatar', 'rest_framework', - 'django_filters', 'rest_framework.authtoken', + 'django_filters', 'users', 'test_records', 'crispy_forms', @@ -120,7 +120,7 @@ STATICFILES_DIRS = [ ] AUTHENTICATION_BACKENDS = ( - 'pgperffarm.auth.AuthBackend', + 'users.views.CustomBackend', ) AUTH_USER_MODEL = 'users.UserProfile' @@ -130,16 +130,13 @@ REST_FRAMEWORK = { # 'rest_framework.permissions.IsAuthenticated', # ), - # 'DEFAULT_AUTHENTICATION_CLASSES': ( - # 'rest_framework.authentication.TokenAuthentication', - # 'rest_framework.authentication.BasicAuthentication', - # 'rest_framework.authentication.SessionAuthentication', - # ), # 'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',), 'DEFAULT_AUTHENTICATION_CLASSES': ( + 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', 'rest_framework.authentication.BasicAuthentication', 'rest_framework.authentication.SessionAuthentication', + # 'rest_framework.authentication.TokenAuthentication', ), 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', 'PAGE_SIZE': 20 diff --git a/web/pgperffarm/urls.py b/web/pgperffarm/urls.py index e0a08b3..3e536a5 100644 --- a/web/pgperffarm/urls.py +++ b/web/pgperffarm/urls.py @@ -13,6 +13,8 @@ Including another URLconf 1. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) """ from django.conf.urls import include, url +from rest_framework_jwt.views import obtain_jwt_token + from rest_framework.authtoken import views from django.contrib import admin from django.views.generic.base import RedirectView @@ -38,7 +40,13 @@ router.register(r'machine', MachineHistoryRecordViewSet, base_name="machine") urlpatterns = [ # url(r'^admin/', admin.site.urls), url(r'^api-auth/', include('rest_framework.urls')), + + url(r'^api-token-auth/', views.obtain_auth_token), + + # login(jwt auth) + url(r'^login/', obtain_jwt_token), + url(r'^machine-token-auth/', MachineAuthToken.as_view()), url(r'^', include(router.urls)), # url(r'status/$', test_record_list, name='test-list'), -- 2.39.5