Commit 35779b1e authored by Evren Kutar's avatar Evren Kutar

use login service and session

- first time in login process, development going on
parent c89ac5ea
...@@ -51,7 +51,12 @@ ...@@ -51,7 +51,12 @@
<script type="text/javascript" src="bower_components/angular-schema-form/dist/bootstrap-decorator.min.js"></script> <script type="text/javascript" src="bower_components/angular-schema-form/dist/bootstrap-decorator.min.js"></script>
<script src="app.js"></script> <script src="app.js"></script>
<!-- login js -->
<script src="login/login.js"></script> <script src="login/login.js"></script>
<script src="login/login_service.js"></script>
<script src="dashboard/dashboard.js"></script> <script src="dashboard/dashboard.js"></script>
<script src="components/version/version.js"></script> <script src="components/version/version.js"></script>
<script src="components/version/version-directive.js"></script> <script src="components/version/version-directive.js"></script>
......
'use strict'; 'use strict';
// TODO: clean console log items // TODO: clean console log items
// TODO: password hash or not??
// TODO: who field can be removed??
angular.module('zaerp.login', ['ngRoute', 'schemaForm']) var login = angular.module('zaerp.login', ['ngRoute', 'schemaForm']);
login.config(['$routeProvider', function ($routeProvider) {
.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/login', { $routeProvider.when('/login', {
templateUrl: 'login/login.html', templateUrl: 'login/login.html',
controller: 'LoginCtrl' controller: 'LoginCtrl'
}); });
}]) }]);
.controller('LoginCtrl', function ($scope, $http, $location, $rootScope) { login.controller('LoginCtrl', function ($scope, $http, $location, $rootScope, AUTH_EVENTS, LoginService) {
$scope.schema = $scope.schema =
{ {
title: "Login", title: "Login",
...@@ -20,7 +21,7 @@ angular.module('zaerp.login', ['ngRoute', 'schemaForm']) ...@@ -20,7 +21,7 @@ angular.module('zaerp.login', ['ngRoute', 'schemaForm'])
type: "email", type: "email",
title: "Email" title: "Email"
}, },
pass: { password: {
type: "string", type: "string",
title: "Password" title: "Password"
}, },
...@@ -34,9 +35,8 @@ angular.module('zaerp.login', ['ngRoute', 'schemaForm']) ...@@ -34,9 +35,8 @@ angular.module('zaerp.login', ['ngRoute', 'schemaForm'])
enum: ["student", "stuff", "dean"] enum: ["student", "stuff", "dean"]
} }
}, },
required: ["email", "pass", "who"] required: ["email", "password", "who"]
}; };
//$scope.fields = ["email", "pass", "who", "remember"];
$scope.model = { $scope.model = {
email: "user@example.com", email: "user@example.com",
remember: false remember: false
...@@ -47,7 +47,7 @@ angular.module('zaerp.login', ['ngRoute', 'schemaForm']) ...@@ -47,7 +47,7 @@ angular.module('zaerp.login', ['ngRoute', 'schemaForm'])
type: "email" type: "email"
}, },
{ {
key: "pass", key: "password",
type: "password" type: "password"
}, },
"remember", "remember",
...@@ -63,6 +63,12 @@ angular.module('zaerp.login', ['ngRoute', 'schemaForm']) ...@@ -63,6 +63,12 @@ angular.module('zaerp.login', ['ngRoute', 'schemaForm'])
if (form.$valid){ if (form.$valid){
$rootScope.loggedInUser = true; $rootScope.loggedInUser = true;
$location.path("/dashboard"); $location.path("/dashboard");
var credentials = {email: form.email, password: form.password};
var loginResponse = LoginService.login(credentials);
console.log(loginResponse);
//$http.post('http://127.0.0.1:8003/#/login', form.email). //$http.post('http://127.0.0.1:8003/#/login', form.email).
// success(function(data, status, headers, config){ // success(function(data, status, headers, config){
// console.log(data); // console.log(data);
......
...@@ -6,12 +6,12 @@ ...@@ -6,12 +6,12 @@
// TODO: login url cheange with correct one // TODO: login url cheange with correct one
angular.factory('LoginService', function ($http, Session) { login.factory('LoginService', function ($http, Session) {
var loginService = {}; var loginService = {};
loginService.login = function (credentials) { loginService.login = function (credentials) {
return $http return $http
.post('http://127.0.0.1:8000/login', credentials) .get('http://127.0.0.1:8000/login', credentials)
.then(function (res) { .then(function (res) {
Session.create(res.data.id, res.data.user.id, Session.create(res.data.id, res.data.user.id,
res.data.user.role); res.data.user.role);
...@@ -19,24 +19,24 @@ angular.factory('LoginService', function ($http, Session) { ...@@ -19,24 +19,24 @@ angular.factory('LoginService', function ($http, Session) {
}); });
}; };
authService.isAuthenticated = function () { loginService.isAuthenticated = function () {
return !!Session.userId; return !!Session.userId;
}; };
authService.isAuthorized = function (authorizedRoles) { loginService.isAuthorized = function (authorizedRoles) {
if (!angular.isArray(authorizedRoles)) { if (!angular.isArray(authorizedRoles)) {
authorizedRoles = [authorizedRoles]; authorizedRoles = [authorizedRoles];
} }
return (authService.isAuthenticated() && return (loginService.isAuthenticated() &&
authorizedRoles.indexOf(Session.userRole) !== -1); loginService.indexOf(Session.userRole) !== -1);
}; };
return authService; return loginService;
}); });
// TODO: initial service not working!! // TODO: initial service not working!!
angular.service('Session', function () { login.service('Session', function () {
this.create = function (sessionId, userId, userRole) { this.create = function (sessionId, userId, userRole) {
this.id = sessionId; this.id = sessionId;
this.userId = userId; this.userId = userId;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment