GitHub sign-in
This commit is contained in:
parent
191a7b6b67
commit
7685cff9ee
5 changed files with 55 additions and 17 deletions
|
@ -86,11 +86,12 @@ export default async function Home({
|
||||||
<ConditionalNull condition={existingUser == null}>
|
<ConditionalNull condition={existingUser == null}>
|
||||||
<h2>Log in with the Fediverse</h2>
|
<h2>Log in with the Fediverse</h2>
|
||||||
<p>If you'd like to participate in this jam, feel free to log in!</p>
|
<p>If you'd like to participate in this jam, feel free to log in!</p>
|
||||||
|
<p>To log in with GitHub, enter "github.com"</p>
|
||||||
<form action="/jams/oauth/login">
|
<form action="/jams/oauth/login">
|
||||||
<input name="instance" placeholder="Instance URL (e.g. "social.besties.house" or "woem.men")" type="text" />
|
<input name="instance" placeholder="Instance URL (e.g. "social.besties.house" or "woem.men")" type="text" />
|
||||||
<input type="submit" />
|
<input type="submit" />
|
||||||
</form>
|
</form>
|
||||||
<p><small>Tested on Mastodon, GoToSocial, Pleroma, and Misskey</small></p>
|
<p><small>Tested on GitHub, Mastodon, GoToSocial, Pleroma, and Misskey</small></p>
|
||||||
</ConditionalNull>
|
</ConditionalNull>
|
||||||
<ConditionalNull condition={existingUser != null}>
|
<ConditionalNull condition={existingUser != null}>
|
||||||
<div>
|
<div>
|
||||||
|
|
|
@ -37,7 +37,10 @@ export async function GET(request: NextRequest) {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Test for user existence
|
// Test for user existence
|
||||||
let tUserExists = await mauth.verifyUser(tUserToken.token_type + " " + tUserToken.access_token);
|
let tUserExists;
|
||||||
|
if (instance == "github.com")
|
||||||
|
tUserExists = await mauth.verifyUser("Bearer " + tUserToken.access_token);
|
||||||
|
else tUserExists = await mauth.verifyUser(tUserToken.token_type + " " + tUserToken.access_token);
|
||||||
|
|
||||||
console.log(tUserExists);
|
console.log(tUserExists);
|
||||||
|
|
||||||
|
@ -65,7 +68,17 @@ export async function GET(request: NextRequest) {
|
||||||
status: 401
|
status: 401
|
||||||
});
|
});
|
||||||
|
|
||||||
let currentUser = {
|
let currentUser;
|
||||||
|
if (instance == "github.com") currentUser = {
|
||||||
|
id: nanoid(21),
|
||||||
|
instance,
|
||||||
|
username: tUserExists.login,
|
||||||
|
admin: false,
|
||||||
|
url: tUserExists.html_url,
|
||||||
|
banned: false, // GitHub should not be on tier1.
|
||||||
|
joined: Date.now()
|
||||||
|
} as UserTable;
|
||||||
|
else currentUser = {
|
||||||
id: nanoid(21),
|
id: nanoid(21),
|
||||||
instance,
|
instance,
|
||||||
username: tUserExists.acct,
|
username: tUserExists.acct,
|
||||||
|
|
|
@ -43,6 +43,7 @@ export async function GET(request: NextRequest) {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check if instance app exists
|
// Check if instance app exists
|
||||||
|
// For github.com, it should
|
||||||
let existingInstanceApp = await db
|
let existingInstanceApp = await db
|
||||||
.selectFrom('apps')
|
.selectFrom('apps')
|
||||||
.where('apps.instance_domain', '=', instance)
|
.where('apps.instance_domain', '=', instance)
|
||||||
|
@ -81,5 +82,7 @@ export async function GET(request: NextRequest) {
|
||||||
cookieStore.set("instance", instance, {
|
cookieStore.set("instance", instance, {
|
||||||
expires: Date.now() + 604800000
|
expires: Date.now() + 604800000
|
||||||
});
|
});
|
||||||
|
if (instance == "github.com")
|
||||||
|
return Response.redirect(`https://${instance}/login/oauth/authorize?client_id=${existingInstanceApp.client_id}&redirect_uri=${existingInstanceApp.redirect_uri}&scope=&state=${Math.random().toString(36).slice(2)}`)
|
||||||
return Response.redirect(`https://${instance}/oauth/authorize?response_type=code&client_id=${existingInstanceApp.client_id}&redirect_uri=${existingInstanceApp.redirect_uri}&scope=read`);
|
return Response.redirect(`https://${instance}/oauth/authorize?response_type=code&client_id=${existingInstanceApp.client_id}&redirect_uri=${existingInstanceApp.redirect_uri}&scope=read`);
|
||||||
}
|
}
|
|
@ -58,11 +58,12 @@ export default async function Home({
|
||||||
<p>Enjoy!</p>
|
<p>Enjoy!</p>
|
||||||
<ConditionalNull condition={existingUser == null}>
|
<ConditionalNull condition={existingUser == null}>
|
||||||
<h2>Log in with the Fediverse</h2>
|
<h2>Log in with the Fediverse</h2>
|
||||||
|
<p>To log in with GitHub, enter "github.com"</p>
|
||||||
<form action="/jams/oauth/login">
|
<form action="/jams/oauth/login">
|
||||||
<input name="instance" placeholder="Instance URL (e.g. "social.besties.house" or "woem.men")" type="text" />
|
<input name="instance" placeholder="Instance URL (e.g. "social.besties.house" or "woem.men")" type="text" />
|
||||||
<input type="submit" />
|
<input type="submit" />
|
||||||
</form>
|
</form>
|
||||||
<p><small>Tested on Mastodon, GoToSocial, Pleroma, and Misskey</small></p>
|
<p><small>Tested on GitHub, Mastodon, GoToSocial, Pleroma, and Misskey</small></p>
|
||||||
</ConditionalNull>
|
</ConditionalNull>
|
||||||
<ConditionalNull condition={existingUser != null}>
|
<ConditionalNull condition={existingUser != null}>
|
||||||
<p>Logged in as <a href={`/jams/user/${existingUser?.id}`}>{existingUser?.username}@{existingUser?.instance}</a> (<a href="/jams/oauth/logout">Logout</a>)</p>
|
<p>Logged in as <a href={`/jams/user/${existingUser?.id}`}>{existingUser?.username}@{existingUser?.instance}</a> (<a href="/jams/oauth/logout">Logout</a>)</p>
|
||||||
|
|
|
@ -73,16 +73,28 @@ export class MastoAuth {
|
||||||
formData.append('client_id', client.client_id);
|
formData.append('client_id', client.client_id);
|
||||||
formData.append('client_secret', client.client_secret);
|
formData.append('client_secret', client.client_secret);
|
||||||
formData.append('redirect_uri', client.redirect_uri);
|
formData.append('redirect_uri', client.redirect_uri);
|
||||||
formData.append('grant_type', 'authorization_code');
|
if (this.instance != "github.com")
|
||||||
|
formData.append('grant_type', 'authorization_code');
|
||||||
formData.append('code', code);
|
formData.append('code', code);
|
||||||
formData.append('scope', 'read');
|
if (this.instance != "github.com")
|
||||||
|
formData.append('scope', 'read');
|
||||||
|
|
||||||
let appRequest;
|
let appRequest;
|
||||||
try {
|
try {
|
||||||
appRequest = await fetch(`https://${this.instance}/oauth/token`, {
|
if (this.instance == "github.com") {
|
||||||
body: formData,
|
appRequest = await fetch(`https://${this.instance}/login/oauth/access_token`, {
|
||||||
method: "post"
|
body: formData,
|
||||||
});
|
method: "post",
|
||||||
|
headers: {
|
||||||
|
"Accept": "application/json"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
appRequest = await fetch(`https://${this.instance}/oauth/token`, {
|
||||||
|
body: formData,
|
||||||
|
method: "post"
|
||||||
|
});
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -92,7 +104,7 @@ export class MastoAuth {
|
||||||
access_token: string,
|
access_token: string,
|
||||||
token_type: string,
|
token_type: string,
|
||||||
scope: string,
|
scope: string,
|
||||||
created_at: number
|
created_at?: number
|
||||||
} = await appRequest.json();
|
} = await appRequest.json();
|
||||||
return reqEntities;
|
return reqEntities;
|
||||||
} else return null;
|
} else return null;
|
||||||
|
@ -101,11 +113,19 @@ export class MastoAuth {
|
||||||
async verifyUser(auth: string) {
|
async verifyUser(auth: string) {
|
||||||
let appRequest;
|
let appRequest;
|
||||||
try {
|
try {
|
||||||
appRequest = await fetch(`https://${this.instance}/api/v1/accounts/verify_credentials`, {
|
if (this.instance == "github.com") {
|
||||||
headers: {
|
appRequest = await fetch(`https://api.${this.instance}/user`, {
|
||||||
"Authorization": auth
|
headers: {
|
||||||
}
|
"Authorization": auth
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
appRequest = await fetch(`https://${this.instance}/api/v1/accounts/verify_credentials`, {
|
||||||
|
headers: {
|
||||||
|
"Authorization": auth
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue