rwandabuild/murugo_api_auth

Package that will use Murugo auth to all 3rd party laravel projects with only API structure

v2.6.2 2023-10-09 08:16 UTC

README

Issues Stars Total Downloads

Package that will be used for Murugo auth to all 3rd party laravel projects with only API structure

Follow the following steps to get started:

1. Install package by running the following command

composer require rwandabuild/murugo_api_auth

2. Include the following variables in config services file

    'murugo' => [
        'client_id' => env('MURUGO_CLIENT_ID'),
        'client_secret' => env('MURUGO_CLIENT_SECRET'),
        'redirect' => env('APP_REDIRECT_URL', 'YOUR LOGIN REDIRECT URL'),
        'murugo_url' => env('MURUGO_URL', 'MURUGO_URL'),
        'murugo_app_key' => env('MURUGO_APP_KEY'),
    ],

3. Dont forget to publish your migration by running the following command, when you want to upgrade

php artisan vendor:publish

4. Use the following migration

php artisan migrate

5. Add method to redirect user to murugo

   use RwandaBuild\MurugoAuth\Facades\MurugoAuth;


    public function redirectToMurugo()
    {
        return MurugoAuth::redirect();
    }

6. Add a callback method to be used after the redirection

   use RwandaBuild\MurugoAuth\Facades\MurugoAuth;


    public function murugoCallback()
    { 
        $murugoUser = MurugoAuth::user()
    }

7. Package also comes with this following method

    /**
     * This one is used by client(mobile) to authenticate murugo users on their 3rd party servers
     */
   use RwandaBuild\MurugoAuth\Facades\MurugoAuth;
   $tokens = [
               'access_token' => 'murugo_user_access_token'],
              'refresh_token' => 'murugo_user_refresh_token',
              'expires_in' => integer
   ],
   $murugoUser = MurugoAuth::userFromToken($tokens);

8. Add relationship between User and Murugo User models

  • Add a Trait built in the package already that makes relationship between User model and Murugo user model
    use RwandaBuild\MurugoAuth\Traits\MurugoAuthHelper;
    class User extends Authenticatable
    {
      use MurugoAuthHelper;
    }
  • To access the murugo user when you already have user model like the following:
    $user = User::find(1);
    // access the relationship
    $murugoUser = $user->murugoUser;
  • When you have a murugo user model and you need to the related user, you can do it in the following way:
$murugoUser = MurugoAuth::user();
// accessing the related user
$user = $murugoUser->user;

NOTES: By default package is using App\User model or App\Models|user for making relationship between your user and murugo user model

9. At this step this is how you refresh tokens

First you should know that the Murugo user model keeps the access_token refresh_token and the token_expires_at , now in case you may need to refresh the token of an existing, just do it in the following way:

$user = User::find(2);

// refreshing murugo user token

$murugoUser = MurugoAuth::refreshToken($user->murugoUser);

By Default package will add the following api routes in your laravel project

  • api/murugo-auth >>> This route will be used to get response sent from murugo and save in your laravel project database
  • api/authenticate-user >>>This route will authenticate user by checking uuid of user and by checking if token is still valid and if true return user object
  • api/logout >>> This route will logout user on murugo server

Follow RwandaBuild bellow and contact us