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

[WIP] docs: Add a Japanese version to the document. #1386

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions docs/pages/translations/ja/basics/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
---
title: "Configuration"
---

# Configuration

This page shows all the options for [`Lucia`](/reference/main/Lucia) to configure Lucia.

```ts
interface Options {
sessionExpiresIn?: TimeSpan;
sessionCookie?: SessionCookieOptions;
getSessionAttributes?: (
databaseSessionAttributes: DatabaseSessionAttributes
) => _SessionAttributes;
getUserAttributes?: (databaseUserAttributes: DatabaseUserAttributes) => _UserAttributes;
}
```

## `sessionExpiresIn`

Configures how long a session stays valid for inactive users. Session expirations are automatically extended for active users. Also see [`TimeSpan`](/reference/main/TimeSpan).

```ts
import { Lucia, TimeSpan } from "lucia";

const lucia = new Lucia(adapter, {
sessionExpiresIn: new TimeSpan(2, "w")
});
```

## `sessionCookie`

Configures the session cookie.

```ts
import { Lucia } from "lucia";

const lucia = new Lucia(adapter, {
sessionCookie: {
name: "session",
expires: false, // session cookies have very long lifespan (2 years)
attributes: {
secure: true,
sameSite: "strict",
domain: "example.com"
}
}
});
```

## `getSessionAttributes()`

Transforms database session attributes, which is typed as `DatabaseSessionAttributes`. The returned object is added to the `Session` object.

```ts
import { Lucia } from "lucia";

const lucia = new Lucia(adapter, {
getSessionAttributes: (attributes) => {
return {
country: attributes.country
};
}
});

declare module "lucia" {
interface Register {
Lucia: typeof lucia;
DatabaseSessionAttributes: DatabaseSessionAttributes;
}
}

interface DatabaseSessionAttributes {
country: string;
}
```

## `getUserAttributes()`

Transforms database user attributes, which is typed as `DatabaseUserAttributes`. The returned object is added to the `User` object.

```ts
import { Lucia } from "lucia";

const lucia = new Lucia(adapter, {
getUserAttributes: (attributes) => {
return {
username: attributes.username
};
}
});

declare module "lucia" {
interface Register {
Lucia: typeof lucia;
DatabaseUserAttributes: DatabaseUserAttributes;
}
}

interface DatabaseUserAttributes {
username: string;
}
```
177 changes: 177 additions & 0 deletions docs/pages/translations/ja/basics/sessions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
---
title: "Sessions"
---

# Sessions

Sessions allow Lucia to keep track of requests made by authenticated users. The ID can be stored in a cookie or used as a traditional token manually added to each request. They should be created and stored on registration and login, validated on every request, and deleted on sign out.

```ts
interface Session extends SessionAttributes {
id: string;
userId: string;
expiresAt: Date;
fresh: boolean;
}
```

## Session lifetime

Sessions do not have an absolute expiration. The expiration gets extended whenever they're used. This ensures that active users remain signed in, while inactive users are signed out.

More specifically, if the session expiration is set to 30 days (default), Lucia will extend the expiration by another 30 days when there are less than 15 days (half of the expiration) until expiration. You can configure the expiration with the `sessionExpiresIn` configuration.

```ts
import { Lucia, TimeSpan } from "lucia";

export const lucia = new Lucia(adapter, {
sessionExpiresIn: new TimeSpan(2, "w") // 2 weeks
});
```

## Define session attributes

Defining custom session attributes requires 2 steps. First, add the required columns to the session table. You can type it by declaring the `Register.DatabaseSessionAttributes` type.

```ts
declare module "lucia" {
interface Register {
Lucia: typeof lucia;
DatabaseSessionAttributes: DatabaseSessionAttributes;
}
interface DatabaseSessionAttributes {
ip_country: string;
}
}
```

You can then include them in the session object with the `getSessionAttributes()` configuration.

```ts
const lucia = new Lucia(adapter, {
getSessionAttributes: (attributes) => {
return {
ipCountry: attributes.ip_country
};
}
});

const session = await lucia.createSession();
session.ipCountry;
```

We do not automatically expose all database columns as

1. Each project has its own code styling rules
2. You generally don't want to expose sensitive data (even worse if you send the entire session object to the client)

## Create sessions

You can create a new session with `Lucia.createSession()`, which takes a user ID and an empty object.

```ts
const session = await lucia.createSession(userId, {});
```

If you have database attributes defined, pass their values as the second argument.

```ts
const session = await lucia.createSession(userId, {
country: "us"
});

declare module "lucia" {
interface Register {
Lucia: typeof lucia;
DatabaseSessionAttributes: DatabaseSessionAttributes;
}
}

interface DatabaseSessionAttributes {
country: string;
}
```

## Validate sessions

Use `Lucia.validateSession()` to validate a session using its ID. This will return an object containing a session and user. Both of these will be `null` if the session is invalid.

```ts
const { session, user } = await lucia.validateSession(sessionId);
```

If `Session.fresh` is `true`, it indicates the session expiration has been extended and you should set a new session cookie. If you cannot always set a new session cookie due to limitations of your framework, set the [`sessionCookie.expires`](/basics/configuration#sessioncookie) option to `false`.

```ts
const { session } = await lucia.validateSession(sessionId);
if (session && session.fresh) {
// set session cookie
}
```

You can use [`Lucia.readSessionCookie()`](/reference/main/Lucia/readSessionCookie) and [`Lucia.readBearerToken()`](/reference/main/Lucia/readBearerToken) to get the session ID from the `Cookie` and `Authorization` header respectively.

```ts
const sessionId = lucia.readSessionCookie("auth_session=abc");
const sessionId = lucia.readBearerToken("Bearer abc");
```

See the [Validate session cookies](/guides/validate-session-cookies) and [Validate bearer tokens](/guides/validate-bearer-tokens) guide for a full example of validating session cookies.

## Session cookies

### Create session cookies

You can create a session cookie for a session with [`Lucia.createSessionCookie()`](/reference/main/Lucia/createSessionCookie). It takes a session and returns a new [`Cookie`](/reference/main/Cookie) instance. You can either use [`Cookie.serialize()`](https://oslo.js.org/reference/cookie/Cookie/serialize) to create `Set-Cookie` HTTP header value, or use your framework's API by accessing the name, value, and session.

```ts
const sessionCookie = lucia.createSessionCookie(session.id);

// set cookie directly
headers.set("Set-Cookie", sessionCookie.serialize());
// use your framework's cookie utility
setCookie(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);
```

### Delete session cookie

You can delete a session cookie by setting a blank cookie created using [`Lucia.createBlankSessionCookie()`](/reference/main/Lucia/createBlankSessionCookie).

```ts
const sessionCookie = lucia.createBlankSessionCookie();

headers.set("Set-Cookie", sessionCookie.serialize());
setCookie(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);
```

## Invalidate sessions

Use `Lucia.invalidateSession()` to invalidate a session. This should be used to sign out users. This will succeed even if the session ID is invalid.

```ts
await lucia.invalidateSession(sessionId);
```

### Invalidate all user sessions

Use `Lucia.invalidateUserSessions()` to invalidate all sessions belonging to a user.

```ts
await lucia.invalidateUserSessions(userId);
```

## Get all user sessions

Use `Lucia.getUserSessions()` to get all sessions belonging to a user. This will return an empty array if the user does not exist. Invalid sessions will be omitted from the array.

```ts
const sessions = await lucia.getUserSessions(userId);
```

## Delete all expired sessions

Use `Lucia.deleteExpiredSessions()` to delete all expired sessions in the database. We recommend setting up a cron-job to clean up your database on a set interval.

```ts
await lucia.deleteExpiredSessions();
```
74 changes: 74 additions & 0 deletions docs/pages/translations/ja/basics/users.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
title: "Users"
---

# Users

While Lucia does not provide APIs for creating and managing users, it still interacts with the user table.

```ts
interface Session extends UserAttributes {
id: string;
}
```

## Create users

When creating users, you can use `generateId()` to generate user IDs, which takes the length of the output string. This will generate a cryptographically secure random string consisting of lowercase letters and numbers.

```ts
import { generateId } from "lucia";

await db.createUser({
id: generateId(15)
});
```

Use Oslo's [`generateRandomString()`](https://oslo.js.org/reference/crypto/generateRandomString) if you're looking for a more customizable option.

```ts
import { generateRandomString, alphabet } from "oslo/crypto";

await db.createUser({
id: generateRandomString(15, alphabet("a-z", "A-Z", "0-9"))
});
```

## Define user attributes

Defining custom session attributes requires 2 steps. First, add the required columns to the user table. You can type it by declaring the `Register.DatabaseUserAttributes` type (must be an interface).

```ts
declare module "lucia" {
interface Register {
Lucia: typeof lucia;
DatabaseUserAttributes: DatabaseUserAttributes;
}
}

interface DatabaseUserAttributes {
username: string;
}
```

You can then include them in the user object with the `getUserAttributes()` configuration.

```ts
const lucia = new Lucia(adapter, {
getUserAttributes: (attributes) => {
return {
username: attributes.username
};
}
});

const { user } = await lucia.validateSession();
if (user) {
const username = user.username;
}
```

We do not automatically expose all database columns as

1. Each project has its own code styling rules
2. You generally don't want to expose sensitive data such as hashed passwords (even worse if you send the entire user object to the client)
Loading