Get user Token and store into Localstorage


#1

Please help me,

I try to get user token after login authentication but failed.
I am using Backendless.com as Backend service.
Here my code…

user_service.js

userLogin(userData) 
    {
        let body = JSON.stringify({ login: userData.email, password: userData.password });
        let headers = new Headers(this.Backend_Header);
        let options = new RequestOptions({ headers: headers });
        return this.http.post(this.Backend_Login_Url, body, options)
            .map( res => res.json());
    }

login.js

doLogin(form) {
        this.submitted = true;
        if (form.valid) {
            this.userService.userLogin(this.login)
                .subscribe(data => {
                    console.log('data Login with Success', data);
                    this.user = data;
                    console.log('this.user Login with Success', this.user);
                    this.userId = this.user.objectId;
                    console.log('this.userId Login with Success', this.userId); // Ok
                    this.dataToken = data.userToken;                   
                    console.log('this.dataToken Login with Success', this.dataToken);  // undefined
                    this.userToken = data.user-token;
                    console.log('this.userToken Login with Success', this.userToken); // Reference Error : token is not defined
                    this.storage.set(this.HAS_LOGGED_IN, true);
                    this.events.publish('user:login');
                    this.keyId = data.objectId;
                    this.storage.set('userStorage', JSON.stringify(data));
                    this.storage.set('userId', this.keyId);
                    this.nav.setRoot(UserPage, { userId: this.userId });
                },
                err => { alert(err), console.log('login.js error', err) }
                );
            // error =>  this.errorMessage = <any>error);
        }
    } 

my data response from backend after login success =

Object {lastLogin: 1463630976919, created: 1456890510000, name: "carlson", ___class: "Users", user-token: "0D0D86CB-0EDB-1924-FF8D-540463359000"…}
___class : "Users"
__meta : "{"relationRemovalIds":{},"selectedProperties": ["__updated__meta","password","created","name","___class","ownerId","updated","objectId","email"],"relatedObjects":{}}"
created : 1456890510000
email : "sample@gmail.com"
lastLogin : 1463630976919
name : "sample"
objectId : "C8FE36A2-9396-13C5-FF9C-8F8EF7F07900"
ownerId : null
updated : null
user-token : "0D0D86CB-0EDB-1924-FF8D-540463359000"
__proto__: Object 

Thks in advance for kindly help.


#2


download angular-local-storage.js
and then try this
You need to use localStorageService in your controller and LocalStorageModule in your angular app


#3

Why are you recommending Angular1 directives for an Angular2 project?


#4

Thks @chetanbuddh and @rapropos for ur feedback,

But my problem now is how to get the user token and save it to localstorage ??
Any one can help me to get and save the Token from backend service ??

Thks.