-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
915 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { AppContext } from "../../mod.ts"; | ||
import { proxySetCookie } from "../../utils/cookies.ts"; | ||
import type { CreateEditSessionResponse } from "../../utils/openapi/vcs.openapi.gen.ts"; | ||
|
||
interface Props { | ||
publicProperties: Record<string, unknown>; | ||
} | ||
|
||
async function action( | ||
props: Props, | ||
req: Request, | ||
ctx: AppContext, | ||
): Promise<CreateEditSessionResponse | null> { | ||
const { vcs } = ctx; | ||
|
||
try { | ||
const response = await vcs["POST /api/sessions"]({}, { | ||
body: { | ||
public: { | ||
...props.publicProperties, | ||
}, | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to create session: ${response.status}`); | ||
} | ||
|
||
proxySetCookie(response.headers, ctx.response.headers, req.url); | ||
|
||
return await response.json(); | ||
} catch (error) { | ||
console.error("Error creating VTEX session:", error); | ||
return null; | ||
} | ||
} | ||
|
||
export default action; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { AppContext } from "../../mod.ts"; | ||
import { parseCookie } from "../../utils/vtexId.ts"; | ||
|
||
export interface DeleteSession { | ||
logOutFromSession: string; | ||
} | ||
|
||
const mutation = `mutation LogOutFromSession($sessionId: ID) { | ||
logOutFromSession(sessionId: $sessionId) @context(provider: "vtex.store-graphql@2.x") | ||
}`; | ||
|
||
interface Props { | ||
sessionId: string; | ||
} | ||
|
||
async function action( | ||
{ sessionId }: Props, | ||
req: Request, | ||
ctx: AppContext, | ||
): Promise<DeleteSession | null> { | ||
const { io } = ctx; | ||
const { cookie, payload } = parseCookie(req.headers, ctx.account); | ||
|
||
if (!payload?.sub || !payload?.userId) { | ||
return null; | ||
} | ||
|
||
try { | ||
return await io.query<DeleteSession, { sessionId: string }>({ | ||
query: mutation, | ||
variables: { sessionId }, | ||
}, { headers: { cookie } }); | ||
} catch (error) { | ||
console.error(error); | ||
return null; | ||
} | ||
} | ||
|
||
export default action; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { AppContext } from "../../mod.ts"; | ||
import { proxySetCookie } from "../../utils/cookies.ts"; | ||
import type { CreateEditSessionResponse } from "../../utils/openapi/vcs.openapi.gen.ts"; | ||
import { parseCookie } from "../../utils/vtexId.ts"; | ||
|
||
interface Props { | ||
publicProperties: Record<string, unknown>; | ||
} | ||
|
||
async function action( | ||
props: Props, | ||
req: Request, | ||
ctx: AppContext, | ||
): Promise<CreateEditSessionResponse | null> { | ||
const { vcs } = ctx; | ||
const { cookie } = parseCookie(req.headers, ctx.account); | ||
|
||
try { | ||
const response = await vcs["PATCH /api/sessions"]({}, { | ||
body: { | ||
public: { | ||
...props.publicProperties, | ||
}, | ||
}, | ||
headers: { cookie }, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to edit session: ${response.status}`); | ||
} | ||
|
||
proxySetCookie(response.headers, ctx.response.headers, req.url); | ||
|
||
return await response.json(); | ||
} catch (error) { | ||
console.error("Error editing VTEX session:", error); | ||
return null; | ||
} | ||
} | ||
|
||
export default action; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { AppContext } from "../../mod.ts"; | ||
import type { GetSessionResponse } from "../../utils/openapi/vcs.openapi.gen.ts"; | ||
import { parseCookie } from "../../utils/vtexId.ts"; | ||
|
||
interface Props { | ||
/** | ||
* Items are the keys of the values you wish to get. They follow the format namespace1.key1,namespace2.key2. | ||
* | ||
* If you wish to recover the data sent on Create new session, it should be public.{key}, replacing {key} with the name of the custom property you created. Following the example request presented in Create new session, it would be public.variable1,public.variable2. | ||
* | ||
* If you want to retrieve all keys from Session Manager, you can use the wildcard operator (*) as a value for this query parameter. | ||
*/ | ||
items: string[]; | ||
} | ||
|
||
async function loader( | ||
props: Props, | ||
req: Request, | ||
ctx: AppContext, | ||
): Promise<GetSessionResponse | null> { | ||
const { vcs } = ctx; | ||
const { cookie } = parseCookie(req.headers, ctx.account); | ||
|
||
try { | ||
const response = await vcs["GET /api/sessions"]({ | ||
items: props.items.join(","), | ||
}, { | ||
headers: { cookie }, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to get session: ${response.status}`); | ||
} | ||
|
||
return await response.json(); | ||
} catch (error) { | ||
console.error("Error getting VTEX session:", error); | ||
return null; | ||
} | ||
} | ||
|
||
export default loader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { AppContext } from "../../mod.ts"; | ||
import type { LoginSessionsInfo } from "../../utils/types.ts"; | ||
import { parseCookie } from "../../utils/vtexId.ts"; | ||
|
||
const query = `query getUserSessions { | ||
loginSessionsInfo { | ||
currentLoginSessionId | ||
loginSessions { | ||
id | ||
cacheId | ||
deviceType | ||
city | ||
lastAccess | ||
browser | ||
os | ||
ip | ||
fullAddress | ||
firstAccess | ||
} | ||
} | ||
}`; | ||
|
||
async function loader( | ||
_props: unknown, | ||
req: Request, | ||
ctx: AppContext, | ||
): Promise<LoginSessionsInfo | null> { | ||
const { io } = ctx; | ||
const { cookie, payload } = parseCookie(req.headers, ctx.account); | ||
|
||
if (!payload?.sub || !payload?.userId) { | ||
return null; | ||
} | ||
|
||
try { | ||
const data = await io.query< | ||
{ loginSessionsInfo: LoginSessionsInfo }, | ||
null | ||
>( | ||
{ query }, | ||
{ headers: { cookie } }, | ||
); | ||
|
||
return data.loginSessionsInfo; | ||
} catch (e) { | ||
console.error(e); | ||
return null; | ||
} | ||
} | ||
|
||
export default loader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.