---
title: "SessionService"
generated: true
---
<GenerationInfo sourceFile="packages/core/src/service/services/session.service.ts" sourceLine="33" packageName="@vendure/core" />

Contains methods relating to <DocsLink href="/reference/typescript-api/entities/session#session">Session</DocsLink> entities.

```ts title="Signature"
class SessionService implements EntitySubscriberInterface, OnApplicationBootstrap {
    constructor(connection: TransactionalConnection, configService: ConfigService, orderService: OrderService, jobQueueService: JobQueueService, requestContextService: RequestContextService)
    onApplicationBootstrap() => ;
    createNewAuthenticatedSession(ctx: RequestContext, user: User, authenticationStrategyName: string) => Promise<AuthenticatedSession>;
    createAnonymousSession() => Promise<CachedSession>;
    getSessionFromToken(sessionToken: string) => Promise<CachedSession | undefined>;
    serializeSession(session: AuthenticatedSession | AnonymousSession) => CachedSession;
    setActiveOrder(ctx: RequestContext, serializedSession: CachedSession, order: Order) => Promise<CachedSession>;
    unsetActiveOrder(ctx: RequestContext, serializedSession: CachedSession) => Promise<CachedSession>;
    setActiveChannel(serializedSession: CachedSession, channel: Channel) => Promise<CachedSession>;
    deleteSessionsByUser(ctx: RequestContext, user: User) => Promise<void>;
    deleteSessionsByActiveOrderId(ctx: RequestContext, activeOrderId: ID) => Promise<void>;
    triggerCleanSessionsJob(batchSize: number) => ;
    cleanExpiredSessions(ctx: RequestContext, batchSize: number) => ;
}
```
* Implements: EntitySubscriberInterface, OnApplicationBootstrap



<div className="members-wrapper">

### constructor

<MemberInfo kind="method" type={`(connection: <a href='/reference/typescript-api/data-access/transactional-connection#transactionalconnection'>TransactionalConnection</a>, configService: ConfigService, orderService: <a href='/reference/typescript-api/services/order-service#orderservice'>OrderService</a>, jobQueueService: <a href='/reference/typescript-api/job-queue/job-queue-service#jobqueueservice'>JobQueueService</a>, requestContextService: <a href='/reference/typescript-api/request/request-context-service#requestcontextservice'>RequestContextService</a>) => SessionService`}   />


### onApplicationBootstrap

<MemberInfo kind="method" type={`() => `}   />


### createNewAuthenticatedSession

<MemberInfo kind="method" type={`(ctx: <a href='/reference/typescript-api/request/request-context#requestcontext'>RequestContext</a>, user: <a href='/reference/typescript-api/entities/user#user'>User</a>, authenticationStrategyName: string) => Promise<<a href='/reference/typescript-api/entities/authenticated-session#authenticatedsession'>AuthenticatedSession</a>>`}   />

Creates a new <DocsLink href="/reference/typescript-api/entities/authenticated-session#authenticatedsession">AuthenticatedSession</DocsLink>. To be used after successful authentication.
### createAnonymousSession

<MemberInfo kind="method" type={`() => Promise<<a href='/reference/typescript-api/auth/session-cache-strategy#cachedsession'>CachedSession</a>>`}   />

Create an <DocsLink href="/reference/typescript-api/entities/anonymous-session#anonymoussession">AnonymousSession</DocsLink> and caches it using the configured <DocsLink href="/reference/typescript-api/auth/session-cache-strategy#sessioncachestrategy">SessionCacheStrategy</DocsLink>,
and returns the cached session object.
### getSessionFromToken

<MemberInfo kind="method" type={`(sessionToken: string) => Promise<<a href='/reference/typescript-api/auth/session-cache-strategy#cachedsession'>CachedSession</a> | undefined>`}   />

Returns the cached session object matching the given session token.
### serializeSession

<MemberInfo kind="method" type={`(session: <a href='/reference/typescript-api/entities/authenticated-session#authenticatedsession'>AuthenticatedSession</a> | <a href='/reference/typescript-api/entities/anonymous-session#anonymoussession'>AnonymousSession</a>) => <a href='/reference/typescript-api/auth/session-cache-strategy#cachedsession'>CachedSession</a>`}   />

Serializes a <DocsLink href="/reference/typescript-api/entities/session#session">Session</DocsLink> instance into a simplified plain object suitable for caching.
### setActiveOrder

<MemberInfo kind="method" type={`(ctx: <a href='/reference/typescript-api/request/request-context#requestcontext'>RequestContext</a>, serializedSession: <a href='/reference/typescript-api/auth/session-cache-strategy#cachedsession'>CachedSession</a>, order: <a href='/reference/typescript-api/entities/order#order'>Order</a>) => Promise<<a href='/reference/typescript-api/auth/session-cache-strategy#cachedsession'>CachedSession</a>>`}   />

Sets the `activeOrder` on the given cached session object and updates the cache.
### unsetActiveOrder

<MemberInfo kind="method" type={`(ctx: <a href='/reference/typescript-api/request/request-context#requestcontext'>RequestContext</a>, serializedSession: <a href='/reference/typescript-api/auth/session-cache-strategy#cachedsession'>CachedSession</a>) => Promise<<a href='/reference/typescript-api/auth/session-cache-strategy#cachedsession'>CachedSession</a>>`}   />

Clears the `activeOrder` on the given cached session object and updates the cache.
### setActiveChannel

<MemberInfo kind="method" type={`(serializedSession: <a href='/reference/typescript-api/auth/session-cache-strategy#cachedsession'>CachedSession</a>, channel: <a href='/reference/typescript-api/entities/channel#channel'>Channel</a>) => Promise<<a href='/reference/typescript-api/auth/session-cache-strategy#cachedsession'>CachedSession</a>>`}   />

Sets the `activeChannel` on the given cached session object and updates the cache.
### deleteSessionsByUser

<MemberInfo kind="method" type={`(ctx: <a href='/reference/typescript-api/request/request-context#requestcontext'>RequestContext</a>, user: <a href='/reference/typescript-api/entities/user#user'>User</a>) => Promise<void>`}   />

Deletes all existing sessions for the given user.
### deleteSessionsByActiveOrderId

<MemberInfo kind="method" type={`(ctx: <a href='/reference/typescript-api/request/request-context#requestcontext'>RequestContext</a>, activeOrderId: <a href='/reference/typescript-api/common/id#id'>ID</a>) => Promise<void>`}   />

Deletes all existing sessions with the given activeOrder.
### triggerCleanSessionsJob

<MemberInfo kind="method" type={`(batchSize: number) => `}   />

Triggers the clean sessions job.
### cleanExpiredSessions

<MemberInfo kind="method" type={`(ctx: <a href='/reference/typescript-api/request/request-context#requestcontext'>RequestContext</a>, batchSize: number) => `}   />

Cleans expired sessions from the database & the session cache.


</div>
