Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
KU Login
Browse files Browse the repository at this point in the history
  • Loading branch information
whs committed Oct 4, 2015
1 parent f4fbacb commit 673c8d0
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 18 deletions.
1 change: 1 addition & 0 deletions authapi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

urlpatterns = [
url(r'^check', views.UserViewSet.as_view(), name='auth_check'),
url(r'^login', views.LoginViewSet.as_view(), name='auth_login'),
]
37 changes: 33 additions & 4 deletions authapi/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from django.contrib.auth import authenticate, login
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.exceptions import NotAuthenticated
from rest_framework.exceptions import NotAuthenticated, ValidationError, PermissionDenied

class UserViewSet(APIView):
"""Validate current user session
This API is used to check whether the current user is logged in
and to retrieve information about the user.
Check whether the current user is logged in and retrieve
information about the user.
It could be accessed at :http:get:`/api/auth/check`
Expand All @@ -23,4 +24,32 @@ def get(self, request):
if request.user.is_authenticated():
return Response(request.user.get_username())
else:
raise NotAuthenticated()
raise NotAuthenticated()

class LoginViewSet(APIView):
"""Log a user in with username/password combination
It could be accessed at :http:post:`/api/auth/login`
"""
def post(self, request):
"""Log a user in
Args:
request: Django Rest Framework request object
Return:
Username or 403
"""
if 'username' not in request.data or 'password' not in request.data:
raise ValidationError('Specify username and password')

user = authenticate(username=request.data['username'], password=request.data['password'])

if not user:
raise PermissionDenied('Cannot log you in')
if not user.is_active:
raise PermissionDenied('User disabled')

login(request, user)
return Response(user.get_username())
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ backports.ssl-match-hostname==3.4.0.2
certifi==2015.9.6.2
Django==1.8.4
django-allauth==0.23.0
django-imapauth==0.1
djangorestframework==3.2.3
docutils==0.12
Jinja2==2.8
Expand Down
6 changes: 4 additions & 2 deletions social/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
'rest_framework',
'authapi',
'ui',
'newsfeed'
'newsfeed',
'group',
"User"
)
Expand Down Expand Up @@ -112,9 +112,11 @@
STATIC_URL = '/static/'

AUTHENTICATION_BACKENDS = (
'imapauth.backends.IMAPBackend',
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend'
'allauth.account.auth_backends.AuthenticationBackend',
)
IMAPAUTH_HOST = 'nontri.ku.ac.th'

SITE_ID = 2
LOGIN_REDIRECT_URLNAME = '/static/index.html'
Expand Down
27 changes: 26 additions & 1 deletion ui/static/app/login.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
(function(){

var app = angular.module('app.login', []);
var app = angular.module("app.login", []);

app.controller("LoginController", function($scope, $rootScope, Restangular, $state){
var endpoint = Restangular.all("auth/login");

$scope.auth = {
"username": "",
"password": ""
};

$scope.login = function(){
$scope.error = null;
$scope.loggingIn = true;
endpoint.post($scope.auth).then(function(data){
$rootScope.user = data;
$state.go('root.newsfeed');
}, function(request){
if ( request.data["detail"] ) {
$scope.error = request.data.detail;
}else{
$scope.error = request.statusText;
}
$scope.loggingIn = false;
});
}
});

})();
3 changes: 2 additions & 1 deletion ui/static/app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ app.config(function($stateProvider, $urlRouterProvider) {
})
.state('login', {
url: '/login',
templateUrl: 'templates/login.html'
templateUrl: 'templates/login.html',
controller: 'LoginController'
});
});

Expand Down
24 changes: 14 additions & 10 deletions ui/static/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ <h4>Please login</h4>
<a class="btn btn-primary" ng-click="kulogin=true">KU Login</a>
<a class="btn btn-primary" href="{{app_base}}accounts/facebook/login">Facebook Login</a>
</div>
<form ng-show="kulogin">
<form ng-show="kulogin" ng-submit="login()">
<h4>KU Login</h4>
<div class="form-group">
<label for="student_id">Student ID</label>
<input type="text" class="form-control" id="student_id" placeholder="56xxxxxxxx" pattern="[45][0-9]{9}">
<div class="alert alert-danger" ng-bind="error" ng-if="error"></div>
<div class="alert alert-info" ng-if="loggingIn">Working...</div>
<div ng-hide="loggingIn">
<div class="form-group">
<label for="student_id">Student ID</label>
<input type="text" class="form-control" id="student_id" placeholder="b56xxxxxxxx" pattern="b[45][0-9]{9}" required ng-model="auth.username">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" required ng-model="auth.password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<a class="btn btn-default" ng-click="kulogin=false">Cancel</a>
</div>
<div class="form-group">
<label for="student_id">Password</label>
<input type="password" class="form-control" id="student_id">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<a class="btn btn-default" ng-click="kulogin=false">Cancel</a>
</form>
</div>
</div>
Expand Down

0 comments on commit 673c8d0

Please sign in to comment.