User Login

This example shows how you can implement user login on the server side using the DID Token.

The example assumes:

important

It is important to always validate the DID Token before using.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Magic;
class UserController extends Controller
{
function login(Request $request){
$did_token = $request->bearerToken();
if ($did_token == null) {
// DIDT is missing from the original HTTP request header.
// You can handle this by remapping it to your application error.
}
try {
// Validate the did token
Magic::token()->validate($did_token);
$issuer = Magic::token()->get_issuer($did_token);
} catch (Throwable $e) {
// DIDT is malformed.
// You can handle this by remapping it to your application error.
report($e);
return false;
}
if ($user_info->issuer != $issuer) {
// Unauthorized login due to issuer mismatch.You can handle this by remapping
// it to your application error.
}
// Final step, call your application logic to log in the user.
}
}