gpt4 book ai didi

javascript - session 后访问 session 用户名如何在其他 Controller 中进行身份验证

转载 作者:行者123 更新时间:2023-11-28 06:40:44 25 4
gpt4 key购买 nike

我正在使用 Ember-simple-auth 来验证我的路线,并且工作正常。我有:

login - login route
index - after login in ok

我正在使用 ember-cli-notification 弹出此消息:

"Welcome USER NAME"

像这样,在 IndexController 中:

export default Ember.Controller.extend({
init () {
this.notifications.addNotification({
message: 'Welcome USER NAME',
type: 'success',
autoClear: true,
clearDuration: 5000
});
}
});

我想问...如何访问当前登录用户的属性?我在一个完全不同的 Controller 中,所以我不知道如何做到这一点......

而且..我想问是否可能..我正在为此使用init函数..我这样做对吗?

谢谢。

最佳答案

使用 session 服务。如果您在响应中返回当前用户的详细信息,它们将保留在 session.data.authenticated 中。

// app/controllers/index.js
import Ember from 'ember';

export default Ember.Controller.extend({
session: Ember.inject.service(),
init () {
this.notifications.addNotification({
message: `Welcome ${this.get('session.data.authenticated.name')}`,
type: 'success',
autoClear: true,
clearDuration: 5000
});
}
});

引用Docs & Dummy app

希望对你有帮助...

关于javascript - session 后访问 session 用户名如何在其他 Controller 中进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33847332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com