Home > OS >  Logging out user from Google OAuth
Logging out user from Google OAuth

Time:08-02

I have a Vapor app and I am using Imperial for oauth login. When a user signs in, everything works just fine. When a user clicks a button to sign out, he's being redirected to the login screen, but then if he presses 'login with google' again, it automatically signs him in, without authenticating him, which is an issue, since what if the user wants to use a different account? there is no way to re-enter credentials. I understood I need to logout out of Google and not only out of my app, but how can I achieve it with Vapor?

I've tried to destroy the session, thinking it might destroy the 'caching' of the authentication, but It didn't work. The code for destroying the session:

logout function inside the UserController in the Vapor app:

 func logout(req:Request) -> HTTPResponseStatus {
    req.session.destroy()
    print("Session destroyed.")
    return .ok
}

CodePudding user response:

Your diagnosis that you remain logged into google is correct. However, before implementing a forced logout of google, have you considered the scenario?

  1. The first login to your vapor app triggers a successful login to google because you weren't already logged into google.
  2. At some point in the vapor app session, you start something else, say in a different browser tab, that uses the current google account.
  3. You then log out of google when you log out of your app.
  4. Your partial task at 2. is in a somewhat unknown state.

If you really do want to log out of google, then it is as simple as adding this line to your existing route:

let response = try await req.client.get("https://google.com/...")

There is more information in Vapor docs here. You will need to research the exact URI to use.

  • Related