I am making a mobile application with a laravel API, and I saw that since Laravel 5.3(?) they added something called "Passport" which handles OAuth2/verification for an application, so I thought I would give that a try. I followed several different explanations for how to get it working after I completed the setup using the Laravel Passport documentation (https://laravel.com/docs/5.4/passport). Right now this is the code for what I've come up with based off other tutorials/ SO articles
1.) Controller for creating the user/oAuth2 client
class OAuthController extends Controller
{
public function registerUser(Request $request){
$email = $request->email;
$password = $request->password;
$name = $request->name;
$user = User::create([
'name' => $name,
'email' => $email,
'password' => bcrypt($password)
]);
$oAuthClient = new OAuthClient();
$oAuthClient->user_id = $user->id;
$oAuthClient->id = $user->email;
$oAuthClient->name = $user->name;
$oAuthClient->secret = base64_encode(hash_hmac('sha256',$password, 'secret', true));
$oAuthClient->password_client=1;
$oAuthClient->redirect = '';
$oAuthClient->personal_access_client = 0;
$oAuthClient->revoked = 0;
$oAuthClient->save();
return response()->json(['message', 'User successfully created']);
}
}
2.) The model I made to reference the oauth_clients table
use Illuminate\Database\Eloquent\Model;
class OAuthClient extends Model
{
protected $table = 'oauth_clients';
}
3.) I changed the oauth_clients table primarykey from incrementing integer to the users email. I was basically just following this SO article Laravel Passport Password Grant Tokens: own mobile app
4.) Once I have created the user/oauth_client, retrieve the token through POSTMAN w/ post request to oauth/token with parameters
The thing is, this feels really wrong to me. the oauth_clients has a user_id column on it, so it really leads me to believe when attempting to get the oauth_client token that I should be able to do some post request where it will take the user and then get the associated oauth_client, right?
In my mind what makes sense for how I should be able to use Passport for user authentication for my mobile app is as follows: 1.) Register new user
2.) When registering user, create oauth_client for that user
3.) On login, once user email/pw is verified, look for oauth_client and then retrieve the oath_client token
4.) Use oauth_client token on any requests to API going forward to verified authenticated user.
Is this the right way to think of it?? I'm sure it's apparent, but this process has me confused so any guidance will be greatly appreciated.