description |
---|
Decodes a Decentralized ID Token from a Base64 string into a tuple of its individual components. |
decode(didToken)
didToken
(String): A Decentralized ID Token generated by a Magic user on the client-side.
[string, Claim]
: containing the [proof, claim]
that composes the DID Token. The claim
, while given as a JSON string, is automatically parsed to the following interface:
interface Claim {
iat: number; // Issued At Timestamp
ext: number; // Expiration Timestamp
iss: string; // Issuer of DID Token
sub: string; // Subject
aud: string; // Audience
nbf: number; // Not Before Timestamp
tid: string; // DID Token ID
add: string; // (optional) Misc additional signed data
}
As a convenience, the above interface is available to your code in TypeScript:
import { Claim } from '@magic-sdk/admin';
const { Magic } = require('@magic-sdk/admin');
const mAdmin = new Magic('SECRET_API_KEY');
...
/* Gets the DID token from a login request */
app.get('/login', async (req: any, res: any) => {
/*
Assumes DIDToken was passed in the Authorization header
in the standard `Bearer {token}` format.
*/
const DIDToken = req.headers.authorization.substring(7);
const [proof, claim] = mAdmin.token.decode(DIDToken);
console.log(proof); // => string
console.log(claim);
/*
=> {
iat: number,
ext: number,
iss: string,
sub: string,
aud: string,
nbf: number,
tid: string
}
*/
});