kullar84/mobileid

Estonian MobileID identity provider for laravel

Installs: 23

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 1

Type:package

0.2 2018-01-16 21:54 UTC

This package is auto-updated.

Last update: 2024-12-24 20:09:38 UTC


README

Laravel mobile ID authentication

Installation

composer require kullar84/mobileid

The package is now installed. If you are using laravel version < 5.5, add the following line in your config/app.php providers section:

kullar84\mobileid\MobileIDServiceProvider::class,

Create new middleware group to Kernel.php

protected $middlewareGroups = [
        'session' => [
            \Illuminate\Session\Middleware\StartSession::class,
        ],
    ];

Add new routes that uses this middleware

Route::group(['middleware' => 'session'], function () {
    Route::post('mlogin', 'AuthController@mlogin')->name('mlogin');
    Route::post('domlogin', 'AuthController@doMLogin')->name('domlogin');
});

Create required methods

use kullar84\mobileid\MobileIDAuthenticate;


protected function mlogin(Request $request)
{
	$this->validate(
		$request, [
			'phone' => 'required|numeric|exists:users,phone',
		]
	);

	$phone = $request->input('phone');

	$auth = new MobileIDAuthenticate();

	$response = $auth->startAuth($phone);

	return response()->json($response);
}

protected function doMLogin(Request $request)
{
	//https://www.id.ee/?id=36373
	$auth = new MobileIDAuthenticate();
	$response = $auth->checkAuthStatus();

	if ($response->isError()) {
		return response()->json(['error' => true, 'message' => $response->error]);
	} elseif ($response->isPending()) {
		return response()->json(['pending' => true, 'message' => 'Please wait! You will be redirected shortly']);
	} elseif ($response->isSuccess()) {
		$phone = $request->input('phone');

		$user = User::findUserByPhone($phone);

		if ($user !== null) {
			$token = $user->createToken('Token')->accessToken;

			return $this->respondWithToken($token);
		}
	}

	return response()->json(['message' => 'Unauthorized'], 422);
}