Correct way to handle email/password sign up, in addition to using Laravel Socialite for OAuth?

34 Views Asked by At

I'm building an iOS app with Laravel backend.

I'm using Firebase Authentication to handle the following authentication types: Google, Apple, and email/password sign up.

I've set up Google and Apple sign up by utilizing Laravel Socialite, however it doesn't look like Socialite supports an email/password driver, so I can't use this code for email/password authentication:

class AuthController extends Controller
{
    public function login(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'provider_name' => 'required|string|in:google,apple',
            'access_token' => 'required|string',
        ]);
 
        if ($validator->fails()) {
            Log::info($validator->errors());
            return;
        }

        $providerName = $request->input('provider_name');
        $token = $request->input('access_token');

        $providerUser = Socialite::driver($providerName)
            ->userFromToken($token);

        $user = User::where('provider_name', $providerName)
            ->where('provider_id', $providerUser->id)
            ->first();

        if ($user == null)
        {
            $user = new User;
            $user->name = $providerUser->name;
            $user->email = $providerUser->email;
            $user->provider_name = $providerName;
            $user->provider_id = $providerUser->id;
            $user->save();
        }

        $token = $user
            ->createToken("token")
            ->accessToken;

        return response()->json([
            "status" => "success",
            "data" => [
                "token" => $token,
                "user" => $user
            ],
        ]);
    }
}

So what's the "right" way of ALSO handling email/password sign up?

I'm at the point where I've the user is signed in on the iOS app and the account has been created through Firebase. I just don't know what to do to actually pass the user data to the Laravel backend and handle authentication/tokens as I do with Google/Apple authentication.

0

There are 0 best solutions below