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

Guide on how to get user data #1028

Merged
merged 25 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7b06714
Started the guide to handle user data
Esther-Lita Sep 19, 2023
c555c9c
User data on the frontend
Esther-Lita Sep 19, 2023
1e48169
Updated the guide to get user data
Esther-Lita Sep 25, 2023
0139579
Merge branch 'main' into handling-user-onboarding
Esther-Lita Sep 25, 2023
0062da7
Merge branch 'main' into handling-user-onboarding
Esther-Lita Sep 26, 2023
e9eafe3
updated information
Esther-Lita Sep 27, 2023
216c8d4
Merge branch 'main' into handling-user-onboarding
Esther-Lita Sep 27, 2023
ed4dcac
Update docs/docs/guides/tutorials/userId.mdx
Esther-Lita Sep 27, 2023
ce73c40
Update docs/docs/guides/tutorials/userId.mdx
Esther-Lita Sep 27, 2023
7448a3e
Update docs/docs/guides/tutorials/userId.mdx
Esther-Lita Sep 27, 2023
1d73441
Update docs/docs/guides/tutorials/userId.mdx
Esther-Lita Sep 27, 2023
24b3d93
Update docs/docs/guides/tutorials/userId.mdx
Esther-Lita Sep 27, 2023
e5a742d
Update docs/docs/guides/tutorials/userId.mdx
Esther-Lita Sep 27, 2023
9b089e2
Update docs/sidebars.js
Esther-Lita Sep 27, 2023
5573225
Merge branch 'main' into handling-user-onboarding
Esther-Lita Sep 27, 2023
b807ab7
Restructured the guide
Esther-Lita Sep 27, 2023
d6e098e
Merge branch 'main' into handling-user-onboarding
Esther-Lita Sep 28, 2023
1a8a34f
Update docs/docs/guides/tutorials/userData.mdx
Esther-Lita Oct 5, 2023
4069daf
Update docs/docs/guides/tutorials/userData.mdx
Esther-Lita Oct 5, 2023
2758b25
Merge branch 'main' into handling-user-onboarding
Esther-Lita Oct 5, 2023
ecc1f96
Update docs/docs/guides/tutorials/userData.mdx
Esther-Lita Oct 5, 2023
de679f3
Merge branch 'main' into handling-user-onboarding
Esther-Lita Oct 5, 2023
cc8b27a
Merge branch 'main' into handling-user-onboarding
Esther-Lita Oct 9, 2023
3e4c4a8
Updated the guide
Esther-Lita Oct 9, 2023
e8d5b4f
Merge branch 'main' into handling-user-onboarding
lfleischmann Oct 11, 2023
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
91 changes: 91 additions & 0 deletions docs/docs/guides/tutorials/userId.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
---
title: Getting user Data
Esther-Lita marked this conversation as resolved.
Show resolved Hide resolved
sidebar_label: User data
keywords: [user, user ID, userID, email]
---

Since every project can have a different need when handling user onboarding,
we have put together some of the approcaches you can use to
obtain the data needed to identify a specific user.
Esther-Lita marked this conversation as resolved.
Show resolved Hide resolved

## Get user ID from the JWT

On successful authentication, Hanko retreives a JWT contained inside of a cookie. One of the the things
we can get from this JWT is the user ID, we can use [jose library](https://www.npmjs.com/package/jose) to decode
the value of such JWT:

```js
import { cookies } from "next/headers";
import * as jose from "jose";

export async function userId() {
const token = cookies().get("hanko")?.value;
const payload = jose.decodeJwt(token ?? "");

const userID = payload.sub;
return userID;
}
```

## Get user by ID using Hanko Admin API
Esther-Lita marked this conversation as resolved.
Show resolved Hide resolved

The [Hanko Admin API](https://docs.hanko.io/api/admin) provides detailed information about an specific user,
using the user ID as an identifier. In the Hanko Cloud Settings, you have the option to enable the Admin API
(it's a Pro feature) and create an `API Key`, when this last one is created the `secret` will be shown to you
only one time, make sure you store it in a safe place.

The Admin API is protected and you're gonna need to send the `secret`
inside the `headers` in your `fetch`request:
Esther-Lita marked this conversation as resolved.
Show resolved Hide resolved

```js
async function getUserData() {
const adminAPI = process.env.ADMIN_API;
const adminSecret = process.env.ADMIN_SECRET;
const options = {
method: "GET",
headers: {
Authorization: `Bearer ${adminSecret}`,
},
};

fetch(`${adminAPI}/users/${userID}`, options)
.then((response) => response.json())
.then((response) => console.log(response))
.catch((err) => console.error(err));
}

const userData = await getUserData();
console.log("user data:", userData);
```

## Get user data on the Frontend

If you only need the user email or the user id for a frontend usage, you can add this code to the same file where we are
registering the Hanko-Auth component:

```js
"use client";

import { Hanko } from "@teamhanko/hanko-elements";

const hankoApi = process.env.NEXT_PUBLIC_HANKO_API;
const hanko = new Hanko(hankoApi);

async function getEmail() {
const userData = await hanko?.user.getCurrent();
const email = userData.email;

console.log("user Data:", userData);
console.log("email:", email);
Esther-Lita marked this conversation as resolved.
Show resolved Hide resolved
}
```

And call `getEmail()` inside of the callback used to redirect after a successful authentication:
Esther-Lita marked this conversation as resolved.
Show resolved Hide resolved

```js
const redirectAfterLogin = useCallback(() => {
router.replace("/dashboard");
getEmail();
}, [router]);
```
Esther-Lita marked this conversation as resolved.
Show resolved Hide resolved

4 changes: 1 addition & 3 deletions docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ const sidebars = {
{
type: "category",
label: "Tutorials",
items: [
"guides/tutorials/php",
],
items: ["guides/tutorials/php", "guides/tutorials/userId"],
Esther-Lita marked this conversation as resolved.
Show resolved Hide resolved
},
],
};
Expand Down