You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, great library! I use laravel as a restful api with a react frontend, so for me I didn't need a lot of what your library offers. So I made a very basic watered down version that will hopefully be of use to others.
MagicLinkController.php
class MagicLinkController extends Controller
{
public function generate(Request $request)
{
$user = User::find(101);
$magicLink = new MagicLinkService($user);
$magicLink->setRedirectUrl('/somewhere/else');
$url = $magicLink->generate();
return response()->json(['url' => $url], 200);
}
public function check(Request $request, UrlGenerator $urlGenerator)
{
if (!$request->hasValidSignature() || !$urlGenerator->hasCorrectSignature($request) || !$urlGenerator->signatureHasNotExpired($request)) {
throw new AuthorizationException;
}
$user = User::findOrFail($request->id);
return response()->json([
'token' => $user->createToken('web')->plainTextToken,
'user' => $user,
'redirect_to' => $request->redirect_to
], 200);
}
}
MagicLinkService.php
class MagicLinkService
{
public $user;
protected $routeName = 'magic-link';
protected $routeExpires = 30;
protected $redirectUrl = null;
public function __construct(User $user) {
$this->user = $user;
}
public function generate()
{
return URL::temporarySignedRoute(
$this->routeName,
now()->addMinutes($this->routeExpires),
[
'id' => $this->user->getAuthIdentifier(),
'redirect_to' => $this->redirectUrl
]
);
}
public function setRedirectUrl(string $redirectUrl)
{
$this->redirectUrl = $redirectUrl;
}
}
Hi, great library! I use laravel as a restful api with a react frontend, so for me I didn't need a lot of what your library offers. So I made a very basic watered down version that will hopefully be of use to others.
MagicLinkController.php
MagicLinkService.php
routes/api.php
The text was updated successfully, but these errors were encountered: