Skip to content

Commit fb39d95

Browse files
committed
♻️ Improve API
1 parent 0f09a44 commit fb39d95

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

packages/hub/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,16 @@ It's possible to login using OAuth (["Sign in with HF"](https://huggingface.co/d
9696
This will allow you get an access token to use some of the API, depending on the scopes set inside the Space or the OAuth App.
9797

9898
```ts
99-
import { oauthLogin, oauthHandleRedirectIfPresent } from "@huggingface/hub";
99+
import { oauthLoginUrl, oauthHandleRedirectIfPresent } from "@huggingface/hub";
100100

101101
const oauthResult = await oauthHandleRedirectIfPresent();
102102

103103
if (!oauthResult) {
104104
// If the user is not logged in, redirect to the login page
105-
oauthLogin();
105+
window.location.href = await oauthLoginUrl();
106106
}
107107

108-
// You can use oauthResult.accessToken and oauthResult.userInfo
108+
// You can use oauthResult.accessToken, oauthResult.accessTokenExpiresAt and oauthResult.userInfo
109109
console.log(oauthResult);
110110
```
111111

packages/hub/src/lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export * from "./list-files";
1111
export * from "./list-models";
1212
export * from "./list-spaces";
1313
export * from "./oauth-handle-redirect";
14-
export * from "./oauth-login";
14+
export * from "./oauth-login-url";
1515
export * from "./parse-safetensors-metadata";
1616
export * from "./upload-file";
1717
export * from "./upload-files";

packages/hub/src/lib/oauth-login.ts renamed to packages/hub/src/lib/oauth-login-url.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,30 @@ import { createApiError } from "../error";
55
/**
66
* Use "Sign in with Hub" to authenticate a user, and get oauth user info / access token.
77
*
8-
* - First, call `oauthLogin` to trigger the redirect to huggingface.co
9-
* - Then, after the redirect, call `oauthHandleRedirect` to get the oauth user info / access token
8+
* Returns an url to redirect to. After the user is redirected back to your app, call `oauthHandleRedirect` to get the oauth user info / access token.
109
*
11-
* There is also `oauthHandleRedirectIfPresent`, which will call `oauthHandleRedirect` if the URL contains an oauth code.
10+
* When called from inside a static Space with OAuth enabled, it will load the config from the space, otherwise you need to at least specify
11+
* the client ID of your OAuth App.
1212
*
13-
* When called from inside a static Space with OAuth enabled, it will load the config from the space.
13+
* @example
14+
* ```ts
15+
* import { oauthLoginUrl, oauthHandleRedirectIfPresent } from "@huggingface/hub";
16+
*
17+
* const oauthResult = await oauthHandleRedirectIfPresent();
18+
*
19+
* if (!oauthResult) {
20+
* // If the user is not logged in, redirect to the login page
21+
* window.location.href = await oauthLoginUrl();
22+
* }
23+
*
24+
* // You can use oauthResult.accessToken, oauthResult.accessTokenExpiresAt and oauthResult.userInfo
25+
* console.log(oauthResult);
26+
* ```
1427
*
1528
* (Theoretically, this function could be used to authenticate a user for any OAuth provider supporting PKCE and OpenID Connect by changing `hubUrl`,
1629
* but it is currently only tested with the Hugging Face Hub.)
1730
*/
18-
export async function oauthLogin(opts?: {
31+
export async function oauthLoginUrl(opts?: {
1932
/**
2033
* OAuth client ID.
2134
*
@@ -46,12 +59,12 @@ export async function oauthLogin(opts?: {
4659
*
4760
* For Developer Applications, you can add any URL you want to the list of allowed redirect URIs at https://huggingface.co/settings/connected-applications.
4861
*/
49-
redirectUri?: string;
62+
redirectUrl?: string;
5063
/**
5164
* State to pass to the OAuth provider, which will be returned in the call to `oauthLogin` after the redirect.
5265
*/
5366
state?: string;
54-
}): Promise<void> {
67+
}): Promise<string> {
5568
if (typeof window === "undefined") {
5669
throw new Error("oauthLogin is only available in the browser");
5770
}
@@ -81,7 +94,7 @@ export async function oauthLogin(opts?: {
8194
localStorage.setItem("huggingface.co:oauth:nonce", newNonce);
8295
localStorage.setItem("huggingface.co:oauth:code_verifier", newCodeVerifier);
8396

84-
const redirectUri = opts?.redirectUri || window.location.href;
97+
const redirectUri = opts?.redirectUrl || window.location.href;
8598
const state = JSON.stringify({
8699
nonce: newNonce,
87100
redirectUri,
@@ -107,7 +120,7 @@ export async function oauthLogin(opts?: {
107120
.replace(/[/]/g, "_")
108121
.replace(/=/g, "");
109122

110-
window.location.href = `${opendidConfig.authorization_endpoint}?${new URLSearchParams({
123+
return `${opendidConfig.authorization_endpoint}?${new URLSearchParams({
111124
client_id: clientId,
112125
scope: opts?.scopes || "openid profile",
113126
response_type: "code",

0 commit comments

Comments
 (0)