Invader
04/12/2022, 9:57 PMNeedle
04/12/2022, 9:57 PM/title
command!
We have solved your problem?
Click the button below to archive it.Invader
04/12/2022, 9:57 PMjsx
response = await fetch("/api/auth/wallet", {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
walletAddr,
nonce,
signature,
}),
});
const { user, token } = await response.json();
supabase.auth.setAuth(token);
And my API route itself
jsx
const walletApi = async (req: any, res: any) => {
try {
...
// variables stuff above
let { data: user, error }: any = await supabase
.from("users")
.select("*")
.eq('walletAddr', signerAddr)
.eq('nonce', nonce)
const token = jwt.sign(
{
aud: "authenticated",
exp: Math.floor(Date.now() / 1000) + (60 * 60 * 24 * 7),
sub: user.id,
user_metadata: {
id: user.id,
},
role: "authenticated"
},
jwtSecret as string
);
res.status(200).json({ user, token })
} catch (err: any) {
res.status(400).json({ error: err.message });
}
}
export default walletApi
garyaustin
04/12/2022, 10:01 PMNeedle
04/12/2022, 10:01 PMInvader
04/12/2022, 10:02 PMjsx
useEffect(() => {
supabase.auth.onAuthStateChange(async (event, session) => {
console.log("Running an auth state change");
let newUser = supabase.auth.user();
if (newUser) {
await fetch("/api/auth/set", {
method: "POST",
headers: new Headers({ "Content-Type": "application/json" }),
credentials: "same-origin",
body: JSON.stringify({ event, session }),
});
}
setUser(supabase.auth.user() || undefined);
});
});
/api/auth/set
is what sets the cookie afterts
import { NextApiRequest, NextApiResponse } from "next";
import supabase from "lib/pSupabase";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
await supabase.auth.api.setAuthCookie(req, res);
}
if (data.length > 0) {
console.log("Updating nonce");
let data = await supabase
.from("users")
.update({ nonce })
.match({ walletAddr });
console.log(data);
} else if (data.length === 0) {
console.log("Inserting nonce");
let data = await supabase.from("users").insert({ walletAddr, nonce });
}
res.status(200).json({ nonce });
I'm even manually inserting them into the DB - do I need to change this to a signup/ scheme to work?garyaustin
04/12/2022, 10:11 PMInvader
04/12/2022, 10:12 PMgaryaustin
04/12/2022, 10:12 PMInvader
04/12/2022, 10:12 PMgaryaustin
04/12/2022, 10:14 PMInvader
04/12/2022, 10:14 PMgaryaustin
04/12/2022, 10:16 PMInvader
04/12/2022, 10:16 PMauth.users
and my own custom tables - theres no point in using any of the supabase auth functions right?garyaustin
04/12/2022, 10:19 PMInvader
04/12/2022, 10:20 PMgaryaustin
04/12/2022, 10:21 PM