首页 > 解决方案 > 为Android构建时FeathersJS登录错误

问题描述

我正在使用 Ionic 构建一个 Android 应用程序。并使用以下feathers_client.js

const feathers = require('@feathersjs/feathers');
const socketio = require('@feathersjs/socketio-client');
const auth = require('@feathersjs/authentication-client');
const io = require('socket.io-client');

const socket = io('http://mydomain.example:3030');

const feathers_client = feathers();

feathers_client
    .configure(socketio(socket))
    .configure(auth({ storage: window.localStorage }));

module.exports = feathers_client;

当我在浏览器上运行应用程序时,它工作正常。但是当我在 Android 设备上运行它时,我只会得到“NotAuthenticated”。

我假设这是因为 FeathersJS 将 JWT 令牌存储在 window.localStorage 并且这在 Android 应用程序用户空间中不可用。

两个问题:

1)有没有办法告诉 FeathersJS 将此令牌存储在其他地方?

2)如果没有,有人遇到过这种情况并可以为我提供解决方案吗?

顺便说一句,这是我的身份验证代码:

export class SSHSettingsPage implements OnInit {

  public inputEmail: string;
  public inputPassword: string;

  constructor() { }

  ngOnInit() {
  }

  public performLogin($event) {
    let authObj: object = { "strategy": "local", "email": this.inputEmail, "password": this.inputPassword};
    client.authenticate(authObj)
    .then(res => {
      console.log(res);
      window.localStorage.setItem("user",JSON.stringify(res.user));
      window.location.href = "/download";
    })
    .catch(err => {
      console.log(err);
      window.location.href = "/login-error";
    })
  }

}

标签: androidjwtfeathersjsjwt-auth

解决方案


正如配置 API中提到的,该storage选项可以传递一个React Native AsyncStorage的实例:

import {AsyncStorage} from 'react-native';

// Available options are listed in the "Options" section
app.configure(auth({
  storage: AsyncStorage
}))

推荐阅读