File size: 861 Bytes
0ed5b20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { OAuthResult } from "@huggingface/hub"

// return a valid OAuthResult, or else undefined
export function getValidOAuth(rawInput?: any): OAuthResult | undefined {
  try {
    let untypedOAuthResult: any
    try {
      untypedOAuthResult = JSON.parse(rawInput)
      if (!untypedOAuthResult) { throw new Error("no valid serialized oauth result") }
    } catch (err) {
      untypedOAuthResult = rawInput
    }

    const maybeValidOAuth = untypedOAuthResult as OAuthResult

    const accessTokenExpiresAt = new Date(maybeValidOAuth.accessTokenExpiresAt)

    // Get the current date
    const currentDate = new Date()

    if (accessTokenExpiresAt.getTime() < currentDate.getTime()) {
      throw new Error("the serialized oauth result has expired")
    }

    return maybeValidOAuth
  } catch (err) {
    // console.error(err)
    return undefined
  }
}