stablekernel / aqueduct

Dart HTTP server framework for building REST APIs. Includes PostgreSQL ORM and OAuth2 provider.
https://aqueduct.io
BSD 2-Clause "Simplified" License
2.42k stars 280 forks source link

Using third-parties id providers sign-in credentials #938

Open vinnytwice opened 3 years ago

vinnytwice commented 3 years ago

I'm trying to wrap my head around the whole Auth process as I'm building my first API/DB in Aqueduct, but I only found out on docs and other posts how to register users with email/password combination.

@Operation.post()
  Future<Response> createUser(@Bind.body() User user) async {
    if(user.username == null || user.password == null) {
      return Response.badRequest(body: {
        "error": "username and password required."});
    }

    user
      ..salt = AuthUtility.generateRandomSalt()
      ..hashedPassword = authServer.hashPassword(user.password, user.salt);
    return Response.ok( await Query(context, values: user).insert());

  }

In my client app I sign up/in with Google sign-in and use idToken and accessToken that the API to sign-in into Firebase ( which I'm trying to replace with this Aqueduct API ) and I want to do the same in Aqueduct.

Future<User> signInWithGoogle() async {

    final GoogleSignInAccount googleUser = await _googleSignIn.signIn().catchError((e){
      print('Google sign in error: $e');
    });

    final GoogleSignInAuthentication googleAuth =
        await googleUser.authentication;

    final AuthCredential credential = GoogleAuthProvider.credential(
        idToken: googleAuth.idToken, accessToken: googleAuth.accessToken);

    await _firebaseAuth.signInWithCredential(credential);
    return _firebaseAuth.currentUser;
  }

So I'm looking to sign-in with the same idToken/accessToken returned from Google. Can you guys point me in the right direction? It seems I can't find any post about this subject.

How would I create a new user this way? Many thanks. Cheers.