novylevi
04/13/2022, 3:32 PM/signinup
it fails with:
error: "Request failed with status code 401"
status: "FIELD_ERROR"
My code:
js
const code = parseCodeFromUrl();
const provider = 'github';
const environment = {
currentAddress: "http://localhost.com" //<- dev server address
};
const response = await this.axiosInstance.post(
'/auth/signinup',
{
code,
redirectURI: `${environment.currentAddress}/callback/${provider}`,
thirdPartyId: provider,
},
{
headers: {
'rid': 'thirdpartyemailpassword'
}
}
);
rp_st
04/13/2022, 3:37 PM/signinup
it fails with:
error: "Request failed with status code 401"
status: "FIELD_ERROR"
My code:
js
const code = parseCodeFromUrl();
const provider = 'github';
const environment = {
currentAddress: "http://localhost.com" //<- dev server address
};
const response = await this.axiosInstance.post(
'/auth/signinup',
{
code,
redirectURI: `${environment.currentAddress}/callback/${provider}`,
thirdPartyId: provider,
},
{
headers: {
'rid': 'thirdpartyemailpassword'
}
}
);
rp_st
04/13/2022, 3:37 PMnovylevi
04/13/2022, 3:41 PMts
supertokens.init({
framework: 'fastify',
supertokens: {
//FIXME try.supertokens.com is for demo purposes. Replace this with the address of your core instance (sign up on supertokens.com), or self host a core.
connectionURI: 'https://try.supertokens.com',
// apiKey: "IF YOU HAVE AN API KEY FOR THE CORE, ADD IT HERE",
},
appInfo: {
appName: 'TrainNet',
apiDomain: process.env['NODE_ENV'] === 'production' ? 'https://api.trainnet.novy.software' : 'http://api.localhost.com',
websiteDomain: process.env['NODE_ENV'] === 'production' ? 'https://trainnet.novy.software' : 'http://localhost.com',
apiBasePath: '/auth',
websiteBasePath: '/auth'
},
recipeList: [
ThirdPartyEmailPassword.init({
providers: [
//FIXME We have provided you with development keys which you can use for testsing.
Github({
clientId: '467101b197249757c71f',
clientSecret: 'e97051221f4b6426e8fe8d51486396703012f5bd'
}),
]
}),
Session.init() // initializes session features
]
});
import { plugin as supertokensPlugin, errorHandler as supertokensErrHandler } from 'supertokens-node/framework/fastify';
fastify.register(supertokensPlugin);
fastify.setErrorHandler(supertokensErrHandler());
rp_st
04/13/2022, 3:43 PMjs
let github = ThirdPartyEmailPassword.Github({
clientId: "",
clientSecret: "",
});
ThirdPartyEmailPassword.init({
providers: [
{
...github,
get: function (redirectURI: string | undefined, authCodeFromRequest: string | undefined, userContext: any) {
let getResult = github.get(redirectURI, authCodeFromRequest, userContext);
return {
...getResult,
getProfileInfo: async function (authCodeResponse: any, userContext: any) {
try {
return getResult.getProfileInfo(authCodeResponse, userContext);
} catch (err) {
console.log(err);
throw err;
}
}
}
}
},
],
});
novylevi
04/13/2022, 3:44 PMnovylevi
04/13/2022, 3:45 PMrp_st
04/13/2022, 3:45 PMrp_st
04/13/2022, 3:48 PMnovylevi
04/13/2022, 3:49 PMnovylevi
04/13/2022, 3:50 PM<anonymous>] 127.0.0.1 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36: OPTIONS /auth/authorisationurl?thirdPartyId=github - 200
2022-04-13 15:48:53.842 INFO [apps/trainnet-backend/src/index.ts:16 Object.<anonymous>] 127.0.0.1 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36: GET /auth/authorisationurl?thirdPartyId=github - 200
2022-04-13 15:48:57.309 INFO [apps/trainnet-backend/src/index.ts:16 Object.<anonymous>] 127.0.0.1 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36: POST /auth/signinup - 200
rp_st
04/13/2022, 3:51 PMrp_st
04/13/2022, 3:51 PMrp_st
04/13/2022, 4:05 PMnovylevi
04/13/2022, 4:06 PMrp_st
04/13/2022, 4:07 PMrp_st
04/13/2022, 4:08 PM${environment.currentAddress}/auth/callback/${provider}
rp_st
04/13/2022, 4:08 PMnovylevi
04/13/2022, 4:08 PMnovylevi
04/13/2022, 4:10 PMrp_st
04/13/2022, 4:11 PMrp_st
04/13/2022, 4:13 PMnovylevi
04/13/2022, 4:13 PMrp_st
04/13/2022, 4:14 PMnovylevi
04/13/2022, 4:17 PMts
export class SupertokensService {
public axiosInstance: AxiosInstance;
constructor() {
SuperTokens.init({
apiDomain: environment.apiServerAddress,
apiBasePath: '/auth',
});
this.axiosInstance = axios.create({
baseURL: environment.apiServerAddress,
timeout: 10000,
});
SuperTokens.addAxiosInterceptors(this.axiosInstance);
}
public async thirdPartyRedirect(provider: string) {
const intialResponse = await this.axiosInstance.request({
url: `/auth/authorisationurl?thirdPartyId=${provider}`,
method: 'GET',
headers: {
rid: 'thirdpartyemailpassword'
}
});
if (intialResponse.status !== 200 || intialResponse.data.status !== 'OK') throw new Error('Failed to get auth url');
const authUrl = intialResponse.data.url;
const urlObj = new URL(authUrl);
urlObj.searchParams.append('redirect_uri', `${environment.currentAddress}/auth/callback/${provider}`);
const url = urlObj.toString();
window.location.href = url;
}
public async login(provider: string, code: string) {
const response = await this.axiosInstance.post(
'/auth/signinup',
{
code,
redirectURI: `${environment.currentAddress}/auth/callback/${provider}`,
thirdPartyId: provider,
authCodeResponse: {
access_token: 'string',
id_token: 'string'
},
},
{
headers: {
'rid': 'thirdpartyemailpassword'
}
}
);
console.log(response);
}
}
rp_st
04/13/2022, 4:17 PMnovylevi
04/13/2022, 4:18 PMrp_st
04/13/2022, 4:18 PMauthCodeResponse: {
access_token: 'string',
id_token: 'string'
},
rp_st
04/13/2022, 4:18 PMcode
, redirectURI
and thirdPartyId
.rp_st
04/13/2022, 4:18 PMauthCodeResponse
is for mobile apps flow (authorisation code grant flow via PKCE)novylevi
04/13/2022, 4:19 PMrp_st
04/13/2022, 4:20 PMnovylevi
04/13/2022, 4:21 PMnovylevi
04/13/2022, 4:21 PMnovylevi
04/13/2022, 4:22 PMjs
providers: [
{
...github,
get: function (redirectURI: string | undefined, authCodeFromRequest: string | undefined, userContext: any) {
const getResult = github.get(redirectURI, authCodeFromRequest, userContext);
return {
...getResult,
getProfileInfo: async function (authCodeResponse: any, userContext: any) {
try {
const result = await getResult.getProfileInfo(authCodeResponse, userContext);
console.log(result); //<- this one has my login details (id, email, verified)
return result;
} catch (err) {
console.log(err);
throw err;
}
}
};
}
},
],
novylevi
04/13/2022, 4:23 PMrp_st
04/13/2022, 4:23 PMrp_st
04/13/2022, 4:23 PMnovylevi
04/13/2022, 4:25 PMrp_st
04/13/2022, 4:26 PMrp_st
04/13/2022, 4:26 PMrp_st
04/13/2022, 4:26 PMproviders: [
//FIXME We have provided you with development keys which you can use for testsing.
Github({
clientId: '467101b197249757c71f',
clientSecret: 'e97051221f4b6426e8fe8d51486396703012f5bd'
}),
]
novylevi
04/13/2022, 4:26 PMnovylevi
04/13/2022, 4:31 PMrp_st
04/13/2022, 4:32 PMrp_st
04/13/2022, 4:32 PMnovylevi
04/13/2022, 4:36 PM/auth
novylevi
04/13/2022, 4:36 PMrp_st
04/13/2022, 4:36 PMnovylevi
04/13/2022, 4:37 PMrp_st
04/13/2022, 4:38 PMnovylevi
04/13/2022, 4:39 PMSuperTokens is an open source authentication solution offering features like: Different types of login: Email / password, Passwordless (OTP or Magic link based).
Powered by