简体   繁体   English

用于Auth laravel5-angular-material-starter的本地存储

[英]Local Storage for Auth laravel5-angular-material-starter

I am trying to store token in local storage for Auth using the AngularJS ngStorage module. 我正在尝试使用AngularJS ngStorage模块将令牌存储在Auth的本地存储中。 I want to know that how to store in local storage . 我想知道如何存储在本地存储中。

I using a laravel powered angular stack on github: https://github.com/jadjoubran/laravel5-angular-material-starter 我在github上使用laravel供电的角度堆栈: https//github.com/jadjoubran/laravel5-angular-material-starter

I have the following code: 我有以下代码:

create_login_form.component.js create_login_form.component.js

    class CreateLoginFormController{
    constructor(API,ToastService){
        'ngInject';
        this.API = API;
        this.ToastService = ToastService;
    }
    submit(){
     var data = {
       email: this.email,
       password: this.password
     };

      this.API.all('users/login').post(data).then((response) => {

        this.ToastService.show(response.data.token);

          <--- $localStorage -->

      });
   }
}

export const CreateLoginFormComponent = {
    templateUrl: './views/app/components/create_login_form/create_login_form.component.html',
    controller: CreateLoginFormController,
    controllerAs: 'vm',
    bindings: {}
}

create_login_form.component.html create_login_form.component.html

    <form ng-submit="vm.submit()">

    <md-input-container>
      <label>Email</label>
      <input type="text" ng-model="vm.email">
    </md-input-container>

    <md-input-container>
      <label>Password</label>
      <input type="password" ng-model="vm.password">
     </md-input-container>

    <md-button type="submit" class="md-primary md-raised">Login</md-button>

</form>

API.service.js API.service.js

    export class APIService {
    constructor(Restangular, ToastService, $localStorage) {
        'ngInject';
        //content negotiation
        var headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/x.laravel.v1+json'
        };

        return Restangular.withConfig(function(RestangularConfigurer) {
            RestangularConfigurer
                .setBaseUrl('/api/')
                .setDefaultHeaders(headers)
                .setErrorInterceptor(function(response) {
                    if (response.status === 422) {
                        for (var error in response.data.errors) {
                            return ToastService.error(response.data.errors[error][0]);
                        }
                    }
                })
                .addFullRequestInterceptor(function(element, operation, what, url, headers) {
                    if ($localStorage.jwt) {
                        headers.Authorization = 'Bearer ' + $localStorage.jwt;
                    }
                });
        });
    }
}

RoutesConfig.js RoutesConfig.js

    export function RoutesConfig($stateProvider, $urlRouterProvider) {
    'ngInject';

    var getView = function(viewName) {
        return './views/app/pages/' + viewName + '/' + viewName + '.page.html';
    };

    $urlRouterProvider.otherwise('/');

    $stateProvider
        .state('app', {
            abstract: true,
            views: {
                header: {
                    templateUrl: getView('header')
                },
                footer: {
                    templateUrl: getView('footer')
                },
                main: {}
            }
        })
        .state('app.landing', {
            url: '/',
            data: {},
            views: {
                'main@': {
                    templateUrl: getView('landing')
                }
            }
        })

        .state('app.login',{
            url: '/create_login',
            views: {
                'main@': {
                    templateUrl :getView ('create_login')
                }
            }
        })

    .state('app.create_post', {
        url: '/create-post',
        views: {
          'main@': {
            templateUrl: getView('create_post')
          }
        }
      });
}

Try create_login_form.component.js file like this. 试试这样的create_login_form.component.js文件。

class CreateLoginFormController{
    constructor(API,ToastService, $localStorage){
        'ngInject';
        this.API = API;
        this.ToastService = ToastService;
        this.$localStorage = $localStorage;
    }
    submit(){
     var data = {
       email: this.email,
       password: this.password
     };

      this.API.all('users/login').post(data).then((response) => {

        this.ToastService.show(response.data.token);

          this.$localStorage.foo = response.data.token;

      });
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM