This package aims to simplify authentication process required by campaign sites runned by Dicoding.
- Register custom Socialite provider for dicoding.com.
- Implements custom user provider for Laravel authentication system.
- Provide abstraction to handle oAuth callback easily.
- Install the package using composer:
$ composer require dicoding-dev/laravel-dicoding-auth
- Add required configurations to
config/services.php
:Don't forget to add and adjust the values in the'dicoding' => [ 'base_uri' => env('DICODING_BASE_URI'), 'client_id' => env('DICODING_CLIENT_ID'), 'client_secret' => env('DICODING_CLIENT_SECRET'), 'redirect' => '/auth/dicoding/callback' ],
.env
file as well. The base URI should be the base domain without ending slash. - Replace the default authentication guard configuration to
dicoding
:'defaults' => [ 'guard' => 'dicoding', ... ],
- Create a route and action to handle oAuth redirection. Use the following code to redirect user to Dicoding oAuth:
To redirect user directly to Dicoding's registration page, use
return Socialite::driver('dicoding')->redirect();
redirect_to_register
query parameter:return Socialite::driver('dicoding')->with(['redirect_to_register' => 1])->redirect();
- Create another route and action to handle oAuth callback. Use
DicodingDev\LaravelDicodingAuth\OAuthCallbackHandler
trait and implement its abstract methods. e.g.<?php namespace App\Http\Controllers; use DicodingDev\LaravelDicodingAuth\OAuthCallbackHandler; use Exception; class OAuthCallbackController { use OAuthCallbackHandler; protected function handleSuccessfulAuth() { return redirect('/')->with('msg', 'sukses bro'); } protected function handleAccessDenied() { return redirect('/')->with('msg', 'access denied'); } protected function handleFailedAuth(Exception $exception) { return redirect('/')->with('msg', 'error bro'); } }