Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[V2][Sessions] Simplify the logout #726

Closed
LoicPoullain opened this issue May 27, 2020 · 1 comment
Closed

[V2][Sessions] Simplify the logout #726

LoicPoullain opened this issue May 27, 2020 · 1 comment

Comments

@LoicPoullain
Copy link
Member

Problem

Logging users out in Foal is not simple and intuitive. It requires a lot of settings that one might not understand.

Solution

Remove extendLifeTimeOrUpdate and removeSessionCookie.

Examples

Without cookies

Before

import {
  dependency, Context, HttpResponseNoContent, Post,
  Session, TokenRequired
} from '@foal/core';
import { TypeORMStore } from '@foal/typeorm';

export class AuthController {

  @dependency
  store: TypeORMStore;

  @Post('/logout')
  @TokenRequired({
    extendLifeTimeOrUpdate: false,
    store: TypeORMStore,
  })
  async logout(ctx: Context<any, Session>) {
    await this.store.destroy(ctx.session.sessionID);

    return new HttpResponseNoContent();
  }

}

After

import {
  Context, HttpResponseNoContent, Post, TokenOptional
} from '@foal/core';
import { TypeORMStore } from '@foal/typeorm';

export class AuthController {

  @Post('/logout')
  @TokenOptional({
    store: TypeORMStore,
  })
  async logout(ctx: Context) {
    if (ctx.session) {
      await ctx.session.destroy();
    }

    return new HttpResponseNoContent();
  }

}

With cookies

Before

import {
  dependency, Context, HttpResponseNoContent, Post,
  removeSessionCookie, Session, TokenRequired
} from '@foal/core';
import { TypeORMStore } from '@foal/typeorm';

export class AuthController {

  @dependency
  store: TypeORMStore;

  @Post('/logout')
  @TokenRequired({
    cookie: true,
    extendLifeTimeOrUpdate: false,
    store: TypeORMStore,
  })
  async logout(ctx: Context<any, Session>) {
    await this.store.destroy(ctx.session.sessionID);

    const response = new HttpResponseNoContent();
    removeSessionCookie(response);
    return response;
  }

}

After

import {
  Context, HttpResponseNoContent, Post, TokenOptional
} from '@foal/core';
import { TypeORMStore } from '@foal/typeorm';

export class AuthController {

  @Post('/logout')
  @TokenOptional({
    cookie: true,
    store: TypeORMStore,
  })
  async logout(ctx: Context) {
    if (ctx.session) {
      await ctx.session.destroy();
    }

    return new HttpResponseNoContent();
  }

}

Steps

See #659

@LoicPoullain
Copy link
Member Author

Implemented in #659.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

1 participant