# Session & cookies
Ts.ED provides two decorators to get Session and Cookies values in your controller.
# Installation
Before using the Session and Cookies, we need to install a module like express-session (opens new window) but you can use another module which follows the same convention.
npm install --save express-session
WARNING
The default server-side session storage, MemoryStore, is purposely not designed for a production environment. It will leak memory under most conditions, does not scale past a single process, and is meant for debugging and developing.
For a list of stores, see compatible session stores (opens new window).
# Configuration
Edit your Server and add these lines:
import {PlatformApplication} from "@tsed/common";
import {Configuration, Inject} from "@tsed/di";
import "@tsed/platform-express";
import compress from "compression";
import cookieParser from "cookie-parser";
import session from "express-session";
import methodOverride from "method-override";
@Configuration({
middlewares: [
cookieParser(),
compress(),
methodOverride(),
session({
secret: "keyboard cat",
resave: false,
saveUninitialized: true,
cookie: {secure: true}
})
]
})
export class Server {
@Inject()
app: PlatformApplication;
public $beforeRoutesInit(): void | Promise<any> {
this.app.getApp().set("trust proxy", 1); // trust first proxy
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Usage
# Session
# Get value
import {Session, BodyParams} from "@tsed/platform-params";
import {Get, Post} from "@tsed/schema";
import {Controller} from "@tsed/di";
import {Returns} from "@tsed/schema";
@Controller("/")
export class MyCtrl {
@Get("/whoami")
whoAmI(@Session() session: any) {
console.log("User in session =>", session.user);
return session.user && session.user.id ? `Hello user ${session.user.name}` : "Hello world";
}
@Post("/login")
@Returns(204)
login(@BodyParams("name") name: string, @Session("user") user: any) {
user.id = "1";
user.name = name;
}
@Post("/logout")
@Returns(204)
logout(@Session("user") user: any) {
user.id = null;
delete user.name;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Set value
import {BodyParams, Controller, Post, Session} from "@tsed/common";
import {Returns} from "@tsed/schema";
@Controller("/")
export class MyCtrl {
@Post("/")
updateSession(@Session() session: any) {
session.count = (session.count || 0) + 1;
return "OK - " + session.count;
}
}
2
3
4
5
6
7
8
9
10
11
# Cookies
# Get value
import {Cookies} from "@tsed/platform-params";
import {Post} from "@tsed/schema";
import {Controller} from "@tsed/di";
import {IUser} from "./interfaces/IUser";
@Controller("/")
class MyCtrl {
@Post("/")
getCookies(@Cookies() cookies: any) {
console.log("Entire cookies", cookies);
}
@Post("/")
getIdInCookies(@Cookies("id") id: string) {
console.log("ID", id);
}
@Post("/")
getObjectInCookies(@Cookies("user") user: IUser) {
console.log("user", user);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Set value
import {BodyParams, Controller, Post, Cookies} from "@tsed/common";
import {Returns} from "@tsed/schema";
@Controller("/")
export class MyCtrl {
@Post("/")
updateSession(@Cookies() cookies: any) {
cookies.count = (cookies.count || 0) + 1;
return "OK - " + cookies.count;
}
}
2
3
4
5
6
7
8
9
10
11
# Initialize session
Sometimes we want to be sure that the session is correctly initialized with the right values.
Let's start by creating a middleware CreateRequestSessionMiddleware in middlewares
directory:
import {Req} from "@tsed/common";
import {Middleware} from "@tsed/platform-middlewares";
@Middleware()
export class CreateRequestSessionMiddleware {
use(@Req() request: Req) {
if (request.session) {
request.session.user = request.session.user || {
id: null
};
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
Then, add this middleware on the server:
import {PlatformApplication} from "@tsed/common";
import {Configuration, Inject} from "@tsed/di";
import "@tsed/platform-express";
import compress from "compression";
import cookieParser from "cookie-parser";
import session from "express-session";
import methodOverride from "method-override";
import {CreateRequestSessionMiddleware} from "./middlewares/CreateRequestSessionMiddleware";
@Configuration({
middlewares: [
cookieParser(),
compress({}),
methodOverride(),
session({
secret: "keyboard cat", // change secret key
resave: false,
saveUninitialized: true,
cookie: {
secure: false // set true if HTTPS is enabled
}
}),
CreateRequestSessionMiddleware
]
})
class Server {
@Inject()
app: PlatformApplication;
public $beforeRoutesInit(): void | Promise<any> {
this.app.getApp().set("trust proxy", 1); // trust first proxy
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Finally, you can read and write values in your controller:
import {Session, BodyParams} from "@tsed/platform-params";
import {Get, Post} from "@tsed/schema";
import {Controller} from "@tsed/di";
import {Returns} from "@tsed/schema";
@Controller("/")
export class MyCtrl {
@Get("/whoami")
whoAmI(@Session() session: any) {
console.log("User in session =>", session.user);
return session.user && session.user.id ? `Hello user ${session.user.name}` : "Hello world";
}
@Post("/login")
@Returns(204)
login(@BodyParams("name") name: string, @Session("user") user: any) {
user.id = "1";
user.name = name;
}
@Post("/logout")
@Returns(204)
logout(@Session("user") user: any) {
user.id = null;
delete user.name;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
In addition, you can add integration tests with SuperTest and @tsed/testing
package.
Here is an example of Rest API test:
TIP
You can find a working example on Express Session here (opens new window).
Last Updated: 2/11/2022, 7:27:21 PM
Other topics
- Session & cookies
- Passport.js
- Keycloak
- Prisma
- TypeORM
- MikroORM
- Mongoose
- GraphQL
- Socket.io
- Swagger
- AJV
- Multer
- Serve static files
- Templating
- Serverless HTTP
- Seq
- OIDC
- Stripe
- Agenda
- Terminus
- Serverless
- IORedis
- Controllers
- Providers
- Model
- JsonMapper
- Middlewares
- Pipes
- Interceptors
- Authentication
- Hooks
- Exceptions
- Throw HTTP Exceptions
- Cache
- Command
- Response Filter
- Injection scopes
- Custom providers
- Lazy-loading provider
- Custom endpoint decorator
- Testing
- Customize 404