Skip to content

Commit

Permalink
chore(docs): add Express sign-in and sign-out route examples to sessi…
Browse files Browse the repository at this point in the history
…on management (#12450)

docs: add Express sign-in and sign-out route examples to session management
  • Loading branch information
kingdavidHub authored Jan 1, 2025
1 parent 48d775a commit 0820bb5
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions docs/pages/getting-started/session-management/login.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,47 @@ Just like in other frameworks, you can also pass a provider to the `signIn` func
</Code.Svelte>
<Code.Express>


The Express package runs server-side and therefore it doesn't make sense to create a "SignIn button component". However, to signin or signout with Express, send a request to the appropriate [REST API Endpoints](/reference/core/types#authaction) from your client (i.e. `/auth/signin`, `/auth/signout`, etc.).

To sign in users with Express, you can create a route that handles the sign-in logic. Here is an example:

```js filename="src/routes/auth.js"
const express = require("express")
const router = express.Router()
const { signIn } = require("../auth")

router.post("/auth/signin", async (req, res) => {
try {
await signIn(req, res)
res.redirect("/dashboard")
} catch (error) {
res.status(500).send("Sign in failed")
}
})

module.exports = router
```

To sign out users with Express, you can create a route that handles the sign-out logic. Here is an example:

```js filename="src/routes/auth.js"
const express = require("express")
const router = express.Router()
const { signOut } = require("../auth")

router.post("/auth/signout", async (req, res) => {
try {
await signOut(req, res)
res.redirect("/")
} catch (error) {
res.status(500).send("Sign out failed")
}
})

module.exports = router
```

</Code.Express>
</Code>

Expand Down Expand Up @@ -352,6 +391,44 @@ Client-side is a bit simpler as we just need to import a button `on:click` handl

The Express package runs server-side and therefore it doesn't make sense to create a "SignIn button component". However, to signin or signout with Express, send a request to the appropriate [REST API Endpoints](/reference/core/types#authaction) from your client (i.e. `/auth/signin`, `/auth/signout`, etc.).

To sign in users with Express, you can create a route that handles the sign-in logic. Here is an example:

```js filename="src/routes/auth.js"
const express = require("express")
const router = express.Router()
const { signIn } = require("../auth")

router.post("/auth/signin", async (req, res) => {
try {
await signIn(req, res)
res.redirect("/dashboard")
} catch (error) {
res.status(500).send("Sign in failed")
}
})

module.exports = router
```

To sign out users with Express, you can create a route that handles the sign-out logic. Here is an example:

```js filename="src/routes/auth.js"
const express = require("express")
const router = express.Router()
const { signOut } = require("../auth")

router.post("/auth/signout", async (req, res) => {
try {
await signOut(req, res)
res.redirect("/")
} catch (error) {
res.status(500).send("Sign out failed")
}
})

module.exports = router
```

</Code.Express>
</Code>

Expand Down

0 comments on commit 0820bb5

Please sign in to comment.