Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:Desktop add standard OAuth2 login support (#4671)
* feat:Desktop add standard OAuth2 login support Signed-off-by: jingyang <3161362058@qq.com> * fix bug Signed-off-by: jingyang <3161362058@qq.com> --------- Signed-off-by: jingyang <3161362058@qq.com>
- Loading branch information
Showing
15 changed files
with
230 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,4 +107,5 @@ enum ProviderType { | |
PASSWORD | ||
OAUTH2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { getGlobalToken } from '@/services/backend/globalAuth'; | ||
import { jsonRes } from '@/services/backend/response'; | ||
import { enableOAuth2 } from '@/services/enable'; | ||
import { OAuth2Type, OAuth2UserInfoType } from '@/types/user'; | ||
import { customAlphabet } from 'nanoid'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { ProviderType } from 'prisma/global/generated/client'; | ||
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz', 12); | ||
|
||
const clientId = process.env.OAUTH2_CLIENT_ID!; | ||
const clientSecret = process.env.OAUTH2_CLIENT_SECRET!; | ||
const tokenUrl = process.env.OAUTH2_TOKEN_URL; | ||
const userInfoUrl = process.env.OAUTH2_USERINFO_URL; | ||
const redirectUrl = process.env.CALLBACK_URL; | ||
|
||
//OAuth2 Support client_secret_post method to obtain token | ||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
try { | ||
if (!enableOAuth2() || !redirectUrl) { | ||
throw new Error('District related env'); | ||
} | ||
|
||
const { code, inviterId } = req.body; | ||
const url = `${tokenUrl}`; | ||
const oauth2Data = (await ( | ||
await fetch(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}, | ||
body: new URLSearchParams({ | ||
code, | ||
client_id: clientId, | ||
client_secret: clientSecret, | ||
grant_type: 'authorization_code', | ||
redirect_uri: redirectUrl | ||
}) | ||
}) | ||
).json()) as OAuth2Type; | ||
const access_token = oauth2Data.access_token; | ||
|
||
if (!access_token) { | ||
return jsonRes(res, { | ||
message: 'Failed to authenticate', | ||
code: 500, | ||
data: 'access_token is null' | ||
}); | ||
} | ||
|
||
const userUrl = `${userInfoUrl}?access_token=${access_token}`; | ||
const response = await fetch(userUrl, { | ||
headers: { | ||
Authorization: `Bearer ${access_token}` | ||
} | ||
}); | ||
if (!response.ok) | ||
return jsonRes(res, { | ||
code: 401, | ||
message: 'Unauthorized' | ||
}); | ||
const result = (await response.json()) as OAuth2UserInfoType; | ||
|
||
const id = result.sub; | ||
const name = result?.nickname || result?.name || nanoid(8); | ||
const avatar_url = result?.picture || ''; | ||
|
||
const data = await getGlobalToken({ | ||
provider: ProviderType.OAUTH2, | ||
id: id + '', | ||
avatar_url, | ||
name, | ||
inviterId | ||
}); | ||
if (!data) | ||
return jsonRes(res, { | ||
code: 401, | ||
message: 'Unauthorized' | ||
}); | ||
return jsonRes(res, { | ||
data, | ||
code: 200, | ||
message: 'Successfully' | ||
}); | ||
} catch (err) { | ||
console.log(err); | ||
return jsonRes(res, { | ||
message: 'Failed to authenticate with GitHub', | ||
code: 500 | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.