mardi 16 février 2016

ember simple auth save user profile in a session

i can't find out why the the function "set" on session "this.get('session').set('name', name);" does not persist after having reloaded the page i followed exactly what was mentioned in http://ift.tt/1o2M3hv

here is my code

//controllers/authentification.js
import Ember from 'ember';

export default Ember.Controller.extend({

session: Ember.inject.service('session'),
actions: {
    authenticate() {
        var this2 = this;
        let {
            email, motDePasse
        } = this.getProperties('email', 'motDePasse');

        this.get('session').authenticate('authenticator:oauth2', email, motDePasse).then(function () {
            this2.store.queryRecord('membre', {
                membre: {
                    email: email
                }
            }).then(function (membre) {
                var nom = membre.get('nom');
                this2.get('session').set('nom', nom);
                console.log('name:', nom);
            });
        }).catch((reason) => {
            this.set('errorMessage', reason.error || reason);
        });
    }
}
});

for the session-store/sessions.js

import Cookie from 'ember-simple-auth/session-stores/cookie';

export default Cookie.extend();

controllers/application.js

import Ember from 'ember';
import SessionService from 'ember-simple-auth/services/session';

export default Ember.Controller.extend({
    session: Ember.inject.service('session'),
    index: function () {
        this.transitionTo('index');
    }
});

template/application.js

<ul class="nav navbar-nav navbar-right">
{{#if session.isAuthenticated}}
   <li>nom :{{session.nom}}</li>
   <li><a {{action 'logout' on="click"}}>Deconnecter</a></li>
{{else}}
   {{#link-to 'authentification' tagName="li"}}<a href>Authentification</a>{{/link-to}}
{{/if}}
</ul>

the first time i authenticate, the variable "nom" appears but once i reload the page, the variable "nom" disappears but the session stille isAuthenticated




Aucun commentaire:

Enregistrer un commentaire