question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Logout and close session

See original GitHub issue

We are using this library along with nest. We use the following code:

import { AuthenticationService } from './authentication.service';
import { Inject } from '@nestjs/common';
import { AcInterface, IdentityBaseRepositoryInterface, IdentitySessionRepositoryInterface, ProfileRepositoryInterface } from '.';
import { EventBus } from '@nestjs/cqrs';
import { LoggingInterface } from 'shared';
import { OidcSettingsRepositoryInterface } from './oidc-settings-repository-interface';
import { IdentitySession, OidcSettings } from '../domain';

import { OIDCStrategy, IProfile, VerifyCallback } from 'passport-azure-ad';
import { Request } from 'express';
import { PassportStrategy } from '@nestjs/passport';
import { AuthenticationServiceInterface } from './authentication.service.interface';

export class OidcPassportStrategy extends PassportStrategy(OIDCStrategy) {

  private static oidcService: AuthenticationServiceInterface;

  constructor(
    private readonly settings: OidcSettings,
    @Inject('oidcService') oidcService: AuthenticationServiceInterface,
  ) {
    super({
      identityMetadata: settings.openIdProviderUrl,
      clientID: settings.clientId,
      responseType: settings.responseType,
      responseMode: settings.responseMode,
      scope: settings.scope,
      redirectUrl: settings.relyingPartyUrl,
      passReqToCallback: true,
      useCookieInsteadOfSession: true,
      cookieEncryptionKeys: [
        {
          key: key,
          iv: iv,
        },
      ],
    },
      OidcPassportStrategy.process,
    );
    OidcPassportStrategy.oidcService = oidcService;
  }

  private static process(req, profile, done) {
    OidcPassportStrategy.oidcService.validateUser(profile); // This stores the session of the users in our system and creates the session
    return done(null, profile.upn, profile);
  }

}

  async configure(_consumer: MiddlewareConsumer) {
    _consumer
      .apply(
        authenticate('azuread-openidconnect', { session: false }),
      )
      .forRoutes({
        path: '/auth/login',
        method: RequestMethod.ALL,
      });
  }

And it works correctly.

However, we can’t find a way to handle the logout. We are able to destroy the sessions in our system, but cannot redirect the user to the OpenID Provider.

We tried this:

import { ApiUseTags, ApiBearerAuth, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { Controller, Inject, Get, HttpCode, Post, Req, Res } from '@nestjs/common';
import { Request, Response } from 'express';

@ApiUseTags('auth')
@ApiBearerAuth()
@Controller('/auth')
export class OidcAuthenticationController {

  constructor(
  ) { }

  @Get('login')
  async login(): Promise<void> {
  }

  @Post('login')
  async oidcLogin(@Req() request: Request) {
    return {
    };
  }

  @Get('logout')
  async oidcLogout(@Req() request: Request, @Res() response: Response) {
    request.logout();
    response.redirect('/api/v1');
  }

}

Could you please point us to the documentation for this?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
chrisschwacommented, Aug 13, 2019

Did you ever find an Fix or this? I have the same issue 😦

0reactions
jasonnuttercommented, Apr 7, 2022

This will be handled by MSAL Node, closing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What are the different ways to log out or kill a session ... - Quora
There's no easy way to logout. Most websites respond to a login by creating a session token as a cookie, which the browser...
Read more >
proper way to logout from a session in PHP - Stack Overflow
session_start(); // Unset all of the session variables. $_SESSION = array(); // If it's desired to kill the session, also delete the session...
Read more >
End session when user closes the tab or browser without ...
Hi,. How to end the session if user closes the tab or browser without logging out. If the user opens the page on...
Read more >
Closing web application with logout or IE close button - MSDN
I designed login and logout for the application. When logout is clicked, it will call a method to clear the session variables and...
Read more >
Linux Kill and Logout Users Command - nixCraft
Learn how to logout Linux users forcefully. You can kill all processes and logout users with the pkill and other Linux commands.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found