2

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

jeanjohny
  • 113
  • 2
  • 7

1 Answers1

0

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&

eytschkay
  • 961
  • 6
  • 9