I got authentication to work! I go to the authentication page like so:
<Button href={getSpeckleAuthPageURL()}> Go to Auth Page </Button>
where the getSpeckleAuthPageURL
returns the url:
export function getSpeckleAuthPageURL() {
// Generate random challenge
var challenge = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
// Save challenge in localStorage
localStorage.setItem(CHALLENGE, challenge)
// Send user to auth page
return `${SERVER_URL}/authn/verify/${APP_ID}/${challenge}`
}
After authentication, i get the access code in my root +layout.ts
file (i am using SvelteKit):
export const ssr = false
export const load = ({ url }) => {
const access_code = url.searchParams.get('access_code')
if (access_code) exchangeAccessCode(access_code)
}
where the exchangeAccessCode
is the exact same as in the tutorial:
// Exchanges the provided access code with a token/refreshToken pair, and saves them to local storage.
export async function exchangeAccessCode(accessCode: string) {
var res = await fetch(`${SERVER_URL}/auth/token/`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
accessCode: accessCode,
appId: APP_ID,
appSecret: APP_SECRET,
challenge: localStorage.getItem(CHALLENGE)
})
})
let data = await res.json()
if (data.token) {
// If retrieving the token was successful, remove challenge and set the new token and refresh token
localStorage.removeItem(CHALLENGE)
localStorage.setItem(TOKEN, data.token)
localStorage.setItem(REFRESH_TOKEN, data.refreshToken)
}
return data
}
Although, now i am wondering, where should i store my APP_SECRET
which is used to get auth tokens? I’m building a public app, where the client interacts directly with the Speckle server with no intermediate server. Is it okay to store APP_SECRET
within the application? If not, where can i then store it so that it can be used in a “token” request? The tutorial stores it in a .env
file not included in the version control, but if APP_SECRET
can be found in the request in the application anyway, then what is the big difference?