Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function() {
- 'use strict';
- angular
- .module('authApp', ['ui.router', 'satellizer','ngFileUpload'])
- .config(function($stateProvider, $urlRouterProvider, $authProvider, $httpProvider, $provide) {
- function redirectWhenLoggedOut($q, $injector) {
- return {
- responseError: function(rejection) {
- // Need to use $injector.get to bring in $state or else we get
- // a circular dependency error
- var $state = $injector.get('$state');
- // Instead of checking for a status code of 400 which might be used
- // for other reasons in Laravel, we check for the specific rejection
- // reasons to tell us if we need to redirect to the login state
- var rejectionReasons = ['token_not_provided', 'token_expired', 'token_absent', 'token_invalid'];
- // Loop through each rejection reason and redirect to the login
- // state if one is encountered
- angular.forEach(rejectionReasons, function(value, key) {
- if(rejection.data.error === value) {
- // If we get a rejection corresponding to one of the reasons
- // in our array, we know we need to authenticate the user so
- // we can remove the current user from local storage
- localStorage.removeItem('user');
- // Send the user to the auth state so they can login
- $state.go('auth');
- }
- });
- return $q.reject(rejection);
- }
- }
- }
- // Setup for the $httpInterceptor
- $provide.factory('redirectWhenLoggedOut', redirectWhenLoggedOut);
- // Push the new factory onto the $http interceptor array
- $httpProvider.interceptors.push('redirectWhenLoggedOut');
- $authProvider.loginUrl = '/api/authenticate';
- $urlRouterProvider.otherwise('/auth');
- $stateProvider
- .state('auth', {
- url: '/auth',
- templateUrl : 'views/login.html',
- controller: 'AuthController as auth',
- })
- .state('profile', {
- url: '/profile',
- templateUrl : 'views/edit-profile.html',
- controller: 'AuthController as auth',
- })
- // route for the signup page
- .state('signup', {
- url: '/signup',
- templateUrl : '../views/register.html'
- })
- .state('home', {
- url: '/home',
- templateUrl: '../views/home.html',
- controller: 'AuthController as auth'
- });
- })
- .run(function($rootScope, $state) {
- // $stateChangeStart is fired whenever the state changes. We can use some parameters
- // such as toState to hook into details about the state as it is changing
- $rootScope.$on('$stateChangeStart', function(event, toState) {
- // Grab the user from local storage and parse it to an object
- var user = JSON.parse(localStorage.getItem('user'));
- // If there is any user data in local storage then the user is quite
- // likely authenticated. If their token is expired, or if they are
- // otherwise not actually authenticated, they will be redirected to
- // the auth state because of the rejected request anyway
- if(user) {
- // The user's authenticated state gets flipped to
- // true so we can now show parts of the UI that rely
- // on the user being logged in
- $rootScope.authenticated = true;
- // Putting the user's data on $rootScope allows
- // us to access it anywhere across the app. Here
- // we are grabbing what is in local storage
- $rootScope.currentUser = user;
- // If the user is logged in and we hit the auth route we don't need
- // to stay there and can send the user to the main state
- if(toState.name === "auth") {
- // Preventing the default behavior allows us to use $state.go
- // to change states
- event.preventDefault();
- // go to the "main" state which in our case is users
- $state.go('profile');
- }
- }
- });
- });
- })();
- (function() {
- 'use strict';
- angular
- .module('authApp')
- .controller('AuthController', AuthController);
- function AuthController($auth, $state, $http, $rootScope,$scope,Upload) {
- var vm = this;
- vm.loginError = false;
- vm.loginErrorText;
- vm.login = function() {
- var credentials = {
- email: vm.email,
- password: vm.password
- }
- // Use Satellizer's $auth service to login
- $auth.login(credentials).then(function(data) {
- return $http.get('api/user').then(function(response) {
- // Stringify the returned data to prepare it
- // to go into local storage
- var user = JSON.stringify(response.data.user);
- // Set the stringified user data into local storage
- localStorage.setItem('user', user);
- // The user's authenticated state gets flipped to
- // true so we can now show parts of the UI that rely
- // on the user being logged in
- $rootScope.authenticated = true;
- $rootScope.username=user.name;
- // Putting the user's data on $rootScope allows
- // us to access it anywhere across the app
- $rootScope.currentUser = response.data.user;
- console.log($rootScope.currentUser.profile_picture);
- // Everything worked out so we can now redirect to
- // the users state to view the data
- $state.go('home');
- });
- // If login is successful, redirect to the users state
- $state.go('users');
- }, function(error) {
- console.log(error);
- });
- }
- vm.logout = function() {
- $auth.logout().then(function() {
- // Remove the authenticated user from local storage
- localStorage.removeItem('user');
- // Flip authenticated to false so that we no longer
- // show UI elements dependant on the user being logged in
- $rootScope.authenticated = false;
- // Remove the current user info from rootscope
- $rootScope.currentUser = null;
- });
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement