【问题标题】:AdonisJs Sharing sessionsAdonisJs 分享会
【发布时间】:2016-10-16 19:45:29
【问题描述】:

我正在使用 AdonisJs 框架为多个用户开发身份验证页面。此页面应允许用户仅在经过身份验证后才能查看个人资料。但是 Adonis 现在正在每个连接到应用程序的用户之间共享会话。因此,一旦有人登录,就会为每个人启用个人资料。

'use strict'

 class UserController {

    * login (request, response) {
        const email = request.input('email')
        const password = request.input('password')
        const login = yield request.auth.attempt(email, password)

        if (login) {
            response.send('Logged In Successfully')
            return
        }

        response.unauthorized('Invalid credentails')
    }

    * profile (request, response) {
    const user = yield request.auth.getUser()

    if (user) {
        response.ok(user)
        return
    }

    response.unauthorized('You must login to view your profile')

	}

}

以下是 config/auth.js 的配置

'use strict'

const Config = use('Config')

module.exports = {

  /*
  |--------------------------------------------------------------------------
  | Authenticator
  |--------------------------------------------------------------------------
  |
  | Authenticator is a combination of HTTP Authentication scheme and the
  | serializer to be used for retrieving users. Below is the default
  | authenticator to be used for every request.
  |
  | Available Schemes - basic, session, jwt, api
  | Available Serializers - Lucid, Database
  |
  */
  authenticator: 'session',

  /*
  |--------------------------------------------------------------------------
  | Session Authenticator
  |--------------------------------------------------------------------------
  |
  | Session authenticator will make use of sessions to maintain the login
  | state for a given user.
  |
  */
  session: {
    serializer: 'Lucid',
    model: 'App/Model/User',
    scheme: 'session',
    uid: 'email',
    password: 'password'
  },

  /*
  |--------------------------------------------------------------------------
  | Basic Auth Authenticator
  |--------------------------------------------------------------------------
  |
  | Basic Authentication works on Http Basic auth header.
  |
  */
  basic: {
    serializer: 'Lucid',
    model: 'App/Model/User',
    scheme: 'basic',
    uid: 'email',
    password: 'password'
  },

  /*
  |--------------------------------------------------------------------------
  | JWT Authenticator
  |--------------------------------------------------------------------------
  |
  | Jwt authentication works with a payload sent with every request under
  | Http Authorization header.
  |
  */
  jwt: {
    serializer: 'Lucid',
    model: 'App/Model/User',
    scheme: 'jwt',
    uid: 'email',
    password: 'password',
    secret: Config.get('app.appKey')
  },

  /*
  |--------------------------------------------------------------------------
  | API Authenticator
  |--------------------------------------------------------------------------
  |
  | Api authenticator authenticates are requests based on Authorization
  | header.
  |
  | Make sure to define relationships on User and Token model as defined
  | in documentation
  |
  */
  api: {
    serializer: 'Lucid',
    model: 'App/Model/Token',
    scheme: 'api'
  }

}

下面是 config/database.js

'use strict'

const Env = use('Env')
const Helpers = use('Helpers')

module.exports = {

  /*
  |--------------------------------------------------------------------------
  | Default Connection
  |--------------------------------------------------------------------------
  |
  | Connection defines the default connection settings to be used while
  | interacting with SQL databases.
  |
  */
  connection: Env.get('DB_CONNECTION', 'pg'),

  /*
  |--------------------------------------------------------------------------
  | Sqlite
  |--------------------------------------------------------------------------
  |
  | Sqlite is a flat file database and can be good choice under development
  | environment.
  |
  | npm i --save sqlite3
  |
  */
  sqlite: {
    client: 'sqlite3',
    connection: {
      filename: Helpers.databasePath('development.sqlite')
    },
    useNullAsDefault: true
  },

  /*
  |--------------------------------------------------------------------------
  | MySQL
  |--------------------------------------------------------------------------
  |
  | Here we define connection settings for MySQL database.
  |
  | npm i --save mysql
  |
  */
  mysql: {
    client: 'mysql',
    connection: {
      host: Env.get('DB_HOST', 'localhost'),
      user: Env.get('DB_USER', 'root'),
      password: Env.get('DB_PASSWORD', ''),
      database: Env.get('DB_DATABASE', 'adonis')
    }
  },

  /*
  |--------------------------------------------------------------------------
  | PostgreSQL
  |--------------------------------------------------------------------------
  |
  | Here we define connection settings for PostgreSQL database.
  |
  | npm i --save pg
  |
  */
  pg: {
    client: 'pg',
    connection: {
      host: Env.get('DB_HOST', 'localhost'),
      user: Env.get('DB_USER', 'correctuser'),
      password: Env.get('DB_PASSWORD', 'correctpassword'),
      database: Env.get('DB_DATABASE', 'correctdb')
    }
  }

}

【问题讨论】:

  • 我只想说阿多尼斯看起来很合法。我想放弃快递转而支持阿多尼斯:D

标签: javascript node.js adonis.js


【解决方案1】:

这是 Adonis 3.0.3 中的一个错误,您应该将 Adonis 升级到最新版本(目前为 3.0.6)。

【讨论】:

  • 我应该如何更新阿多尼斯?我正在运行npm i -g adonis-cli,它以2.1.9 版本在全球范围内安装adonis@
  • 只需运行npm update。顺便说一句,Adonis 4.0 版现已推出,请考虑更新:D
猜你喜欢
  • 1970-01-01
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-01
  • 2021-12-19
  • 2019-12-03
  • 2018-05-26
相关资源
最近更新 更多