π Authentication
A Discord Activity wouldn't feel complete without authenticating users. This allows you to provide a more personalized experience, such as showing your users' usernames, avatars, and more.
Lucky for us, Discord's Embedded App SDK simplifies the process.
Prompt Users to Authenticateβ
Most of our templates and Create Robo starters come with a built-in authentication system via <DiscordContextProvider>
. This component abstracts the authentication process, so you don't have to worry about the small details.
import { DiscordContextProvider } from '../hooks/useDiscordSdk'
import { Activity } from './Activity'
import './App.css'
export default function App() {
return (
<DiscordContextProvider authenticate scope={['identify', 'guilds']}>
<Activity />
</DiscordContextProvider>
)
}
Setting authenticate
to true
will prompt users to authenticate with Discord when they open your activity. You can adjust the scope
array to request additional permissions.
The React Hookβ
Once authenticated, you can access the user's information using the useDiscordSdk
hook.
import { useDiscordSdk } from '../hooks/useDiscordSdk'
export function Profile() {
const { accessToken, authenticated, discordSdk, error, session, status } = useDiscordSdk()
// ...
}
The object returned from useDiscordSdk
includes:
accessToken
: The user's access token. (string)authenticated
: Whether the user is authenticated. (boolean)discordSdk
: The Discord SDK instance. (DiscordSDK)error
: Any errors that occurred during authentication. (Error)session
: The user's session information. (DiscordSession)status
: The status will beauthenticating
during the prompt. (string)
You can use the session
object to access the user's information, such as their username, avatar, and more.
const avatarUri = `https://cdn.discordapp.com/avatars/${session.id}/${session.avatar}.png?size=256`
Loading Stateβ
For convenience, <DiscordContextProvider>
accepts a loadingScreen
prop. This component will be displayed while the user is authenticating.
<DiscordContextProvider loadingScreen={<div>Loading...</div>}>
<Activity />
</DiscordContextProvider>
Vanilla projectsβ
If you're not using a template based on React, you can still authenticate users using the Embedded App SDK directly.