Can not authenticate a user who is not an administrator

谁说胖子不能爱 提交于 2020-08-08 06:40:11

问题


I am building a flutter app and I want users to be able to authenticate using their wordpress credentials.

I am using https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/#description and https://github.com/dreamsoftin/flutter_wordpress to make it easier.

import 'package:flutter/material.dart';
import 'package:flutter_wordpress/flutter_wordpress.dart' as wp;

void main() => runApp(MyApp());

wp.WordPress wordPress = wp.WordPress(
  baseUrl: 'https://sandbox.myfprod.fr/',
  authenticator: wp.WordPressAuthenticator.JWT,
  adminName: '',
  adminKey: '',
);

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Material App Bar'),
        ),
        body: Center(
          child: Container(
            child: Column(
              children: <Widget>[
                TextField(
                  obscureText: false,
                  decoration: InputDecoration(labelText: 'Email'),
                ),
                TextField(
                  obscureText: false,
                  decoration: InputDecoration(labelText: 'Password'),    
            ),
                RaisedButton(
                  child: Text('Login'),
                  onPressed: () {
                    Future<wp.User> response = wordPress.authenticateUser(
                      username: '•••••••••••',
                      password: '•••••••••••',
                    );

                    response.then((user) {
                      print(user);
                    }).catchError((err) {
                      print('Failed to fetch user: $err');
                    });
                  },
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}

This code works only if the user has the role of administrator and I want to authenticate all users regardless of their role.

And I also have another issue : I don't have access to all user properties. For example email.email returns null


回答1:


The library relies on access to /wp-json/wp/v2/users. So allowing everyone to access /wp-json/wp/v2/users in your server/jwt configuration will fix it.

The example app can authenticate the user. The auth function returns status code 200 but fetchUser will prevent the app from completing the login. You can make use of the debugger in lib\flutter_wordpress.dart to understand what happens.

Check what your server answers to the following requests and compare the results (make sure to replace url and email in the request).

GET https:/your.url/wp-json/wp/v2/users/?search=nonadminuser@example.com&

Compare it to the corresponding request for an admin user:

GET https:/your.url/wp-json/wp/v2/users/?search=adminUser@example.com&



来源:https://stackoverflow.com/questions/56060812/can-not-authenticate-a-user-who-is-not-an-administrator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!