virgantara/unida-apps-support

A collection of UNIDA Gontor Apps Components.

v1.0.4 2024-12-18 02:33 UTC

This package is not auto-updated.

Last update: 2025-06-05 02:53:33 UTC


README

License PHP Yii2

Welcome to the Apps Support Components package! This collection of Yii2 components simplifies authentication and token management for your applications. Whether you need OAuth2 integration, token handling, or application-based authentication, this package has got you covered! 🌟

📦 Components Overview

Setup

Put this in your params.php or params-local.php

// Previous params codes
'oauth' => [
    'client_id' => 'your-client-id',
    'client_secret' => 'your-client-secret',
    'baseurl' => 'https://your-oauth-server.com',
    'redirectUri' => 'https://your-app.com/callback',
],

Installation

  1. add this to composer.json "repositories"
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/virgantara/Unida-Apps-Support.git"
        }
    ]
  1. add this to composer.json "require"
    "require": {
        "virgantara/unida-apps-support": "dev-master"        
    }
  1. Update your composer by running this code
composer update -vvv
  1. Open your config/web.php, add the following code in components
'components' => [
    ...
    'tokenService' => [
        'class' => 'virgantara\components\TokenService',
    ],
    'aplikasi' => [
        'class' => 'virgantara\components\AplikasiAuth',
        'baseurl' => $params['oauth']['baseurl'], 
    ],
    'tokenManager' => [
        'class' => 'virgantara\components\TokenManager',
    ],
    'oauth2' => [
        'class' => 'virgantara\components\OAuth2Client',
        'tokenValidationUrl' => $params['oauth']['baseurl'], // Endpoint for token validation
        'tokenRefreshUrl' => $params['oauth']['baseurl'],
        'client_id' => $params['oauth']['client_id'],
        'client_secret' => $params['oauth']['client_secret'],
    ],
]
  1. Open your SiteController.php, add the following codes:
    public function actionAuthCallback()
    {
        try {
            $accessToken = Yii::$app->request->get('access_token');
            $refreshToken = Yii::$app->request->get('refresh_token');

            Yii::$app->tokenService->handleAuthCallback($accessToken, $refreshToken);

            return $this->redirect(['site/index']);
        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }

    protected function handleException($e)
    {
        Yii::$app->session->setFlash('danger', $e->getMessage());
        return $this->redirect(['site/index']);
    }

    public function actionCallback()
    {
        try {
            $receivedJwt = Yii::$app->request->get('state');
            $authCode = Yii::$app->request->get('code');

            Yii::$app->tokenService->handleCallback($receivedJwt, $authCode);

            return $this->redirect(['site/index']);
        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }