surazzarus
surazzarus

Reputation: 792

Reloading the page after redirect

In my application I have a link for logout.

html

<a onclick="myFunction()">Logout</a>

js

function myFunction() {
    document.location.href = 'https://url/oauth/logout?redirect=https://url';
}

The problem I am having is, when I click logout, I immediately redirects to the url. It seems like I am not logged out but after I refresh manually, I get logged out.

I also tried window.reload(); inside myFunction() but it actually doesn't get called at all.

Is there a way to reload the page after the myFunction() is called.

Any help is appreciated. Thanks

Upvotes: 0

Views: 2810

Answers (2)

lumio
lumio

Reputation: 7575

You could fetch the logout endpoint and then reload the page if it was successful like so:

const myFunction = async (event) => {
  event.preventDefault()
  const response = await fetch('https://url/oauth/logout');
  if (response.ok) {
    location.replace('new-url');
    // or location.reload()
  }
}

Upvotes: 1

imvain2
imvain2

Reputation: 15847

Instead of doing a "client side" redirect, try a "server side" redirect in whatever language you are using.

<?php
    Logout Logic here
    header("Location: /login_url");
?>

Upvotes: 0

Related Questions