juliomotol / laravel-auth-timeout

Authentication Timeout for Laravel
MIT License
39 stars 8 forks source link

how to auto request every 15 minutes and get message error when redirect login page? #20

Closed gumpon closed 4 years ago

juliomotol commented 4 years ago

how to auto request every 15 minutes

This is more of a frontend concern.

I'm not sure if it should be a part of the package, but I made a new release v2.2.1 which introduces facades, allowing you to do something like this.

route/web.php

// First, lets create a dummy route where we could check if the user has timed
// out. DON'T put a 'auth.timeout' middleware here. Registering one will cause
// timeout counter to reset. 
Route::get('/auth-timeout-checker', function () {
    // If the user is not yet timed out, we'll return true. Otherwise return 
    // some url to redirect to.
    if (AuthTimeout::check()) {
        return response()->json(true);
    } else {
        return response()->json(['redirect' => route('some/path'), 401]);
    }
});

Somewhere in your .js files

setInterval(() => {
    // Assuming you're using axios, we'll make an ajax call to the route we
    // created to check for authentication.
    axios.get('/auth-timeout-checker')
        // We only need to catch the error, no need for the `then` chain.
        .catch(error => {
            // Check if the reponse status is 401 (authentication error) and
            // the contents has `redirect`.
            if (
                error.response.status === 401 &&
                typeof error.response.data.redirect === 'string'
            ) {
                // Redirect to the given path.
                window.location.href = error.response.data.redirect;
            }
        });
}, 6000); // This will run every 1 min, feel free to tweak to your liking.

get message error when redirect login page

What do you mean by this?

gumpon commented 4 years ago

I want to rendering message in login page when throw authentication exception. (please seem photo)

Thankyou for your support

[Image.jpeg]

Get Outlook for iOShttps://aka.ms/o0ukef


From: Julio Motol notifications@github.com Sent: Monday, October 12, 2020 10:11:21 PM To: juliomotol/laravel-auth-timeout laravel-auth-timeout@noreply.github.com Cc: gumpon gumpon@hotmail.com; Author author@noreply.github.com Subject: Re: [juliomotol/laravel-auth-timeout] how to auto request every 15 minutes and get message error when redirect login page? (#20)

how to auto request every 15 minutes

This is more of a frontend concern.

I'm not sure if it should be a part of the package, but I with the new release v2.2.1https://github.com/juliomotol/laravel-auth-timeout/releases/tag/v2.2.1 which introduces facades, allowing you to do something like this.

route/web.php

// First, lets create a dummy route where we could check if the user has timed // out. DON'T put a 'auth.timeout' middleware here. Registering one will cause // timeout counter to reset. Route::get('/auth-timeout-checker', function () { // If the user is not yet timed out, we'll return true. Otherwise return // some url to redirect to. if (AuthTimeout::check()) { return response()->json(true); } else { return response()->json(['redirect' => route('some/path'), 401]); } });

Somewhere in your .js files

setInterval(() => { // Assuming you're using axios, we'll make an ajax call to the route we // created to check for authentication. axios.get('/auth-timeout-checker') // We only need to catch the error, no need for the then chain. .catch(error => { // Check if the reponse status is 401 (authentication error) and // the contents has redirect. if ( error.response.status === 401 && typeof error.response.data.redirect === 'string' ) { // Redirect to the given path. window.location.href = error.response.data.redirect; } }); }, 6000); // This will run every 1 min, feel free to tweak to your liking.


get message error when redirect login page

What do you mean by this?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/juliomotol/laravel-auth-timeout/issues/20#issuecomment-707179085, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAFFH64OBESMYKIZMNHL4GDSKMMBTANCNFSM4SMIJ7FQ.

juliomotol commented 4 years ago

I don't see any photo. But it I think I get what you intend to do. You can use Session for this.

In the route/web.php...

...
if (AuthTimeout::check()) {
    return response()->json(true);
}else{
    Session::put('timeout_message', 'Sorry you have timed out');
    return response()->json(['redirect' => route('some/path'), 401]);
}
...

You may want want to do the same thing in the middleware. See Redirection.

...
protected function redirectTo($request, $guard = null)
{
    Session::put('timeout_message', 'Sorry you have timed out');
    return route('some/path');
}
...

Then in your blade view...

{{ Session::pull(timeout_message) }}
gumpon commented 4 years ago

I try to request 15 minutes but not work. error-1 error-2 error-3

juliomotol commented 4 years ago

My bad, it should be like this.

// somewhere in the  '/auth-timeout-checker' route declaration...
...
}else{
    Session::put('timeout_message', 'Sorry you have timed out');
    return response()->json(['redirect' => route('some/path')], 401); // 401 should be the 2nd parameter here...
}
...

Also, don't apply the 'auth.timeout' middleware on the '/auth-timeout-checker' route.

gumpon commented 4 years ago

yes, it's work. thankyou