Skip to content

Commit

Permalink
fix(integration): add state param while using discord auth
Browse files Browse the repository at this point in the history
  • Loading branch information
0xevmtxn committed Jan 26, 2023
1 parent 9a67cb0 commit ef390a1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/pages/auth-callback/DiscordAuthCallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ const DiscordAuthCallbackPage: FunctionComponent<AuthCallbackPageProps> = () =>
const navigate = useNavigate()
const [isVerify, setIsVerify] = useState(false)
const [market, setMarket] = useState<UndefinedOr<Market>>()
const isPopup = Boolean(queryParams.popup)

const encodedStateParam: string = queryParams.state
const decodedStateParam: { isPopup: boolean } = JSON.parse(window.atob(decodeURIComponent(encodedStateParam)))
const isPopup: boolean = decodedStateParam.isPopup

const clientId = import.meta.env.VITE_DISCORD_CLIENT_ID
const clientSecret = import.meta.env.VITE_DISCORD_CLIENT_SECRET
Expand Down Expand Up @@ -107,6 +110,7 @@ const DiscordAuthCallbackPage: FunctionComponent<AuthCallbackPageProps> = () =>
const onChange = (v: ChangeEvent<HTMLInputElement>) => {
setAssetName(v.target.value)
}

const onSubmit = () => {
if (!assetName) {
return setError('select guild')
Expand Down
8 changes: 5 additions & 3 deletions src/pages/tokenize-form/DiscordForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ const DiscordForm: FunctionComponent<DiscordFormProps> = ({ isPopup }) => {
const redirectUri = encodeURI((import.meta.env.VITE_DISCORD_AUTH_REDIRECT_URI as string) || '')

const scope = encodeURI('guilds')
const url = `https://discord.com/api/oauth2/authorize?client_id=${clientId}&redirect_uri=${
isPopup ? `${redirectUri}?popup=true` : redirectUri
}&scope=${scope}&response_type=code&prompt=consent`
const popupState: { isPopup: boolean } = {
isPopup
}
const stateParam = encodeURIComponent(window.btoa(JSON.stringify(popupState)))
const url = `https://discord.com/api/oauth2/authorize?client_id=${clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=code&prompt=consent&state=${stateParam}`

window.location.assign(url)
}
Expand Down

0 comments on commit ef390a1

Please sign in to comment.