***

title: "SessionService"
generated: true
---------------

<GenerationInfo sourceFile="packages/core/src/service/services/session.service.ts" sourceLine="36" packageName="@vendure/core" />

Contains methods relating to [Session](/current/framework/reference/typescript-api/entities/session#session) entities.

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

* Implements: EntitySubscriberInterface, OnModuleInit

<div className="members-wrapper">

### onModuleInit

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

### createNewAuthenticatedSession

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

Creates a new [AuthenticatedSession](/current/framework/reference/typescript-api/entities/authenticated-session#authenticatedsession). To be used after successful authentication.

The `apiType` argument records the API the session belongs to. The AuthGuard refuses a session
on the Admin API unless it records the Admin API, so this argument is a security control: pass
it whenever the request context is not the API the caller authenticated against, for example a
REST controller which completes an SSO callback for the Shop API.

When it is omitted, the API is taken from `ctx.apiType` if the context carries a request, which
covers every resolver and every REST route. A context with no request states nothing about the
caller: `RequestContext.empty()` hardcodes `apiType: 'admin'`, `RequestContextService.create()`
takes whatever the caller passed, and `RequestContext.deserialize()` drops the request, so a job
queue worker has none either. A session created from such a context records `shop`, the
lower-privilege API, so that omitting the argument fails closed rather than granting Admin API
access.

### createAnonymousSession

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

Create an [AnonymousSession](/current/framework/reference/typescript-api/entities/anonymous-session#anonymoussession) and caches it using the configured [SessionCacheStrategy](/current/framework/reference/typescript-api/auth/session-cache-strategy#sessioncachestrategy),
and returns the cached session object.

### getSessionFromToken

\<MemberInfo kind="method" type={`(sessionToken: string) => Promise<<a href='/current/framework/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='/current/framework/reference/typescript-api/entities/authenticated-session#authenticatedsession'>AuthenticatedSession</a> | <a href='/current/framework/reference/typescript-api/entities/anonymous-session#anonymoussession'>AnonymousSession</a>) => <a href='/current/framework/reference/typescript-api/auth/session-cache-strategy#cachedsession'>CachedSession</a>`}   />

Serializes a [Session](/current/framework/reference/typescript-api/entities/session#session) instance into a simplified plain object suitable for caching.

### setActiveOrder

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

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

Accepts the session of a deserialized [RequestContext](/current/framework/reference/typescript-api/request/request-context#requestcontext) too, which has no token. The
returned session is re-read from the database, so it does have one, unless the session row
no longer exists, in which case the input is returned as is.

### unsetActiveOrder

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

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

Accepts the session of a deserialized [RequestContext](/current/framework/reference/typescript-api/request/request-context#requestcontext) too, see `setActiveOrder`.

### setActiveChannel

\<MemberInfo kind="method" type={`(serializedSession: <a href='/current/framework/reference/typescript-api/auth/session-cache-strategy#cachedsession'>CachedSession</a>, channel: <a href='/current/framework/reference/typescript-api/entities/channel#channel'>Channel</a>) => Promise<<a href='/current/framework/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='/current/framework/reference/typescript-api/request/request-context#requestcontext'>RequestContext</a>, user: <a href='/current/framework/reference/typescript-api/entities/user#user'>User</a>) => Promise<void>`}   />

Deletes all existing sessions for the given user.

### deleteApiKeySession

\<MemberInfo kind="method" type={`(ctx: <a href='/current/framework/reference/typescript-api/request/request-context#requestcontext'>RequestContext</a>, apiKey: <a href='/current/framework/reference/typescript-api/entities/api-key#apikey'>ApiKey</a>) => Promise<void>`}   />

Deletes session related to API-Key

### deleteSessionsByActiveOrderId

\<MemberInfo kind="method" type={`(ctx: <a href='/current/framework/reference/typescript-api/request/request-context#requestcontext'>RequestContext</a>, activeOrderId: <a href='/current/framework/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='/current/framework/reference/typescript-api/request/request-context#requestcontext'>RequestContext</a>, batchSize: number) => `}   />

Cleans expired sessions from the database & the session cache.

</div>
