J.D Nicholls
Founding Full-Stack Web3 Engineer at @baxusco | Digital nomad ๐ | Mentor ๐จโ๐ซ | Speaker ๐ฃ๏ธ | Developer ๐จโ๐ป | Creator of @proyecto26 #opensource #developer
- Open Source Contributor ๐จโ๐ป
- Game developer (Hobby) ๐ฎ
- Developer who loves UX ๐ซ
- Chocolate lover ๐ซ
- Founding Full-Stack Engineer ๐ท
AT
Like Information, the transfer of valueย will become global, instantaneous, free, and accessibleย to everyone. ๐
Liquid Staking, Lending, DEXes, Bridges, CDP, Yield Aggregator, Derivatives, Synthetic Assets, Insurance, Liquidity Management, Privacy, NFT Lending, Options, Vault Strategies, Prediction Markets, Stablecoins, RWAs, Undercollateralized Lending, Oracles
FrontEnd
BackEnd
Generate DID Token
Display user data
With this approach, you can set the JWTย and cookie to expire in 15 minutes, one month, or whatever is best for your app.
const LoginForm = ({ magic }) => {
const [email, setEmail] = useState();
const onSubmit = (ev) => {
ev.preventDefault()
const idToken = await magic.auth.loginWithEmailOTP({
email,
});
// Call BackEnd to verify idToken and create the JWT token
const userInfo = await magic.user.getInfo(); // Get user data
}
return (
<form onSubmit={onSubmit}>
<input
placeholder="Enter email"
type="email"
name="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<button
type="submit"
>
Sign In
</button>
</form>
);
}
import { Magic } from 'magic-sdk';
const LoginPage = ({ magicAPIKey, rpcUrl }) => {
const [magic, setMagic] = useState();
useEffect(() => {
const newMagic = new Magic(magicAPIKey, {
extensions: [
new SolanaExtension({
rpcUrl,
}),
],
});
setMagic(magic);
}, [])
return (
<LoginForm magic={magic}>
);
}
ย Load Environment Variablesย from your routes.
const signTransaction = useCallback(
async (transaction: Transaction | VersionedTransaction) => {
// Serialize the transaction to a binary format
const serializedVT = transaction.serialize();
// Create a new Transaction object from the serialized transaction
const tx = Transaction.from(serializedVT);
// Define the configuration for serializing the transaction
const serialize: SerializeConfig = {
// Don't require signatures from all signers
requireAllSignatures: false,
// Verify the signatures that are present
verifySignatures: true,
};
// Use the Magic SDK to sign the transaction
const signedTransaction = await magic.solana.signTransaction(
tx,
serialize
);
// Create a new Transaction object from the signed transaction's raw binary data
return Transaction.from(signedTransaction.rawTransaction);
}, [magic]
);
import { Magic, WalletType } from '@magic-sdk/admin';
export const action = async ({ request }: ActionFunctionArgs) => {
const formData = await request.clone().formData();
const idToken = formData.get('idToken')?.toString();
// Load the Magic API key from Server
const magic = await Magic.init(process.env.MAGIC_API_KEY);
magic.token.validate(idToken);
// Grabs the issuer from a DID Token
const issuer = magic.token.getIssuer(idToken);
// Gets user metadata based on the given issuer
const { email } = await magic.users.getMetadataByIssuerAndWallet(
issuer,
WalletType.SOLANA,
);
// Create the JWT token, save it from a session cookie and redirect user
// ...
return redirect('/profile', {
status: 302,
headers: {
'Set-Cookie': await saveLoginCookie(),
},
});
};
By J.D Nicholls
Founding Full-Stack Web3 Engineer at @baxusco | Digital nomad ๐ | Mentor ๐จโ๐ซ | Speaker ๐ฃ๏ธ | Developer ๐จโ๐ป | Creator of @proyecto26 #opensource #developer