Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- 'use strict';
- angular
- .module('app')
- .factory('AuthenticationService', AuthenticationService);
- AuthenticationService.$inject = ['$http', '$cookies', '$rootScope', '$timeout', 'UserService'];
- function AuthenticationService($http, $cookies, $rootScope, $timeout, UserService) {
- var service = {};
- service.Login = Login;
- service.SetCredentials = SetCredentials;
- service.ClearCredentials = ClearCredentials;
- return service;
- function Login(username, password, callback) {
- /* Dummy authentication for testing, uses $timeout to simulate api call
- ----------------------------------------------*/
- $timeout(function () {
- var response;
- UserService.GetByUsername(username)
- .then(function (user) {
- if (user !== null && user.password === password) {
- response = { success: true };
- } else {
- response = { success: false, message: 'Username or password is incorrect' };
- }
- callback(response);
- });
- }, 1000);
- /* Use this for real authentication
- ----------------------------------------------*/
- //$http.post('/api/authenticate', { username: username, password: password })
- // .success(function (response) {
- // callback(response);
- // });
- }
- function SetCredentials(username, password) {
- var authdata = Base64.encode(username + ':' + password);
- $rootScope.globals = {
- currentUser: {
- username: username,
- authdata: authdata
- }
- };
- // set default auth header for http requests
- $http.defaults.headers.common['Authorization'] = 'Basic ' + authdata;
- // store user details in globals cookie that keeps user logged in for 1 week (or until they logout)
- var cookieExp = new Date();
- cookieExp.setDate(cookieExp.getDate() + 7);
- $cookies.putObject('globals', $rootScope.globals, { expires: cookieExp });
- }
- function ClearCredentials() {
- $rootScope.globals = {};
- $cookies.remove('globals');
- $http.defaults.headers.common.Authorization = 'Basic';
- }
- }
- // Base64 encoding service used by AuthenticationService
- var Base64 = {
- keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
- encode: function (input) {
- var output = "";
- var chr1, chr2, chr3 = "";
- var enc1, enc2, enc3, enc4 = "";
- var i = 0;
- do {
- chr1 = input.charCodeAt(i++);
- chr2 = input.charCodeAt(i++);
- chr3 = input.charCodeAt(i++);
- enc1 = chr1 >> 2;
- enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
- enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
- enc4 = chr3 & 63;
- if (isNaN(chr2)) {
- enc3 = enc4 = 64;
- } else if (isNaN(chr3)) {
- enc4 = 64;
- }
- output = output +
- this.keyStr.charAt(enc1) +
- this.keyStr.charAt(enc2) +
- this.keyStr.charAt(enc3) +
- this.keyStr.charAt(enc4);
- chr1 = chr2 = chr3 = "";
- enc1 = enc2 = enc3 = enc4 = "";
- } while (i < input.length);
- return output;
- },
- decode: function (input) {
- var output = "";
- var chr1, chr2, chr3 = "";
- var enc1, enc2, enc3, enc4 = "";
- var i = 0;
- // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
- var base64test = /[^A-Za-z0-9+/=]/g;
- if (base64test.exec(input)) {
- window.alert("There were invalid base64 characters in the input text.n" +
- "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='n" +
- "Expect errors in decoding.");
- }
- input = input.replace(/[^A-Za-z0-9+/=]/g, "");
- do {
- enc1 = this.keyStr.indexOf(input.charAt(i++));
- enc2 = this.keyStr.indexOf(input.charAt(i++));
- enc3 = this.keyStr.indexOf(input.charAt(i++));
- enc4 = this.keyStr.indexOf(input.charAt(i++));
- chr1 = (enc1 << 2) | (enc2 >> 4);
- chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
- chr3 = ((enc3 & 3) << 6) | enc4;
- output = output + String.fromCharCode(chr1);
- if (enc3 != 64) {
- output = output + String.fromCharCode(chr2);
- }
- if (enc4 != 64) {
- output = output + String.fromCharCode(chr3);
- }
- chr1 = chr2 = chr3 = "";
- enc1 = enc2 = enc3 = enc4 = "";
- } while (i < input.length);
- return output;
- }
- };
- (function () {
- 'use strict';
- angular
- .module('app')
- .controller('LoginController', LoginController);
- LoginController.$inject = ['$location', 'AuthenticationService', 'FlashService'];
- function LoginController($location, AuthenticationService, FlashService) {
- var vm = this;
- vm.login = login;
- (function initController() {
- // reset login status
- AuthenticationService.ClearCredentials();
- })();
- function login() {
- vm.dataLoading = true;
- AuthenticationService.Login(vm.username, vm.password, function (response) {
- if (response.success) {
- AuthenticationService.SetCredentials(vm.username, vm.password);
- $location.path('/');
- } else {
- FlashService.Error(response.message);
- vm.dataLoading = false;
- }
- });
- };
- }
- })();
- <div class="col-md-6 col-md-offset-3">
- <h2>Login</h2>
- <form name="form" ng-submit="vm.login()" role="form">
- <div class="form-group" ng-class="{ 'has-error': form.username.$dirty && form.username.$error.required }">
- <label for="username">Username</label>
- <input type="text" name="username" id="username" class="form-control" ng-model="vm.username" required />
- <span ng-show="form.username.$dirty && form.username.$error.required" class="help-block">Username is required</span>
- </div>
- <div class="form-group" ng-class="{ 'has-error': form.password.$dirty && form.password.$error.required }">
- <label for="password">Password</label>
- <input type="password" name="password" id="password" class="form-control" ng-model="vm.password" required />
- <span ng-show="form.password.$dirty && form.password.$error.required" class="help-block">Password is required</span>
- </div>
- <div class="form-actions">
- <button type="submit" ng-disabled="form.$invalid || vm.dataLoading" class="btn btn-primary">Login</button>
- <img ng-if="vm.dataLoading" src="data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQJCgAAACwAAAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkECQoAAAAsAAAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkECQoAAAAsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhHBGqRoKw0R8DYlJd8z0fMDgsGo/IpHI5TAAAIfkECQoAAAAsAAAAABAAEAAAAzIIunInK0rnZBTwGPNMgQwmdsNgXGJUlIWEuR5oWUIpz8pAEAMe6TwfwyYsGo/IpFKSAAAh+QQJCgAAACwAAAAAEAAQAAADMwi6IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQJCgAAACwAAAAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAkKAAAALAAAAAAQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA==" />
- <a href="#!/register" class="btn btn-link">Register</a>
- </div>
- </form>
- </div>
Add Comment
Please, Sign In to add comment