簡體   English   中英

如何使用feathers js身份驗證將客戶端連接到服務器?

[英]How connect client to server with feathers js authentication?

這是我的server.js:

app.use('/users', {
    find(params) {
      return Promise.resolve(Users);
    }
  });
  app.service('authentication').hooks({
  before: {
    create: [
      auth.hooks.authenticate(['local', 'jwt'])
    ]
  }
});
app.service('users').hooks({
  before: {
    create: [
      local.hooks.hashPassword()
    ]
  }
});
/////////////////
app.post('/login', auth.express.authenticate('local', { successRedirect: '/app', failureRedirect: '/login' }));

app.get('/app', (req, res, next) => {
  res.json({ success: true });
});

app.get('/login', (req, res, next) => {
  res.json({ success: false });
});
//////////////

如果我的代碼有錯誤,請誰能幫助我? 我應該在前端的文件中做什么? 我使用reactjs

有幾種方法可以通過feathersjs auth 連接客戶端

最簡單的方法(React / Angular / ...)而不使用特定的庫:

驗證url進行發布請求http:// Your_url:port / authentication

主體

{電子郵件:電子郵件,密碼:密碼,策略:“本地”}

如果請求成功,您將獲得一個帶有accessToken響應 因此,您應該保存該令牌並使用它來訪問受保護的路由。

重要信息 :使用“電子郵件” attr,因為它是默認的身份驗證attr。 您可以根據需要配置它。

希望這可以幫助!

在feathersjs中,具有默認的authentication(login)api是這樣的。

網址: http:// localhost:8080 / authentication

方法:發布

{"strategy": "local","email":"admin@xyz.com","password":"admin123"} {"strategy": "local","email":"admin@xyz.com","password":"admin123"}

比您將獲得您期望的令牌。

響應:

{
    "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6ImFjY2VzcyIsInR5cGUiOiJhY2Nlc3MifQ.eyJ1c2VySWQiOjEsImFjY291bnRJZCI6MSwiaWF0IjoxNTE",
    "code": 200,
    "authentication": "user login successful"
}

之后,將此令牌添加到“ Headers Authorization”中等於令牌的所有其他api調用中。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM