feat(strava): refresh token when expired
This commit is contained in:
@@ -1,26 +0,0 @@
|
|||||||
import { IField, IGlobalVariable } from '@automatisch/types';
|
|
||||||
import qs from 'qs';
|
|
||||||
|
|
||||||
export default async function createAuthData($: IGlobalVariable) {
|
|
||||||
try {
|
|
||||||
const oauthRedirectUrlField = $.app.auth.fields.find(
|
|
||||||
(field: IField) => field.key == 'oAuthRedirectUrl'
|
|
||||||
);
|
|
||||||
const redirectUri = oauthRedirectUrlField.value;
|
|
||||||
const searchParams = qs.stringify({
|
|
||||||
client_id: $.auth.data.consumerKey as string,
|
|
||||||
redirect_uri: redirectUri,
|
|
||||||
approval_prompt: 'force',
|
|
||||||
response_type: 'code',
|
|
||||||
scope: 'read_all,profile:read_all,activity:read_all,activity:write',
|
|
||||||
});
|
|
||||||
|
|
||||||
await $.auth.set({
|
|
||||||
url: `${$.app.baseUrl}/oauth/authorize?${searchParams}`,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
throw new Error(
|
|
||||||
`Error occured while verifying credentials: ${error}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
20
packages/backend/src/apps/strava/auth/generate-auth-url.ts
Normal file
20
packages/backend/src/apps/strava/auth/generate-auth-url.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import { URLSearchParams } from 'node:url';
|
||||||
|
import { IField, IGlobalVariable } from '@automatisch/types';
|
||||||
|
|
||||||
|
export default async function createAuthData($: IGlobalVariable) {
|
||||||
|
const oauthRedirectUrlField = $.app.auth.fields.find(
|
||||||
|
(field: IField) => field.key == 'oAuthRedirectUrl'
|
||||||
|
);
|
||||||
|
const redirectUri = oauthRedirectUrlField.value as string;
|
||||||
|
const searchParams = new URLSearchParams({
|
||||||
|
client_id: $.auth.data.clientId as string,
|
||||||
|
redirect_uri: redirectUri,
|
||||||
|
approval_prompt: 'force',
|
||||||
|
response_type: 'code',
|
||||||
|
scope: 'read_all,profile:read_all,activity:read_all,activity:write',
|
||||||
|
});
|
||||||
|
|
||||||
|
await $.auth.set({
|
||||||
|
url: `${$.app.baseUrl}/oauth/authorize?${searchParams}`,
|
||||||
|
});
|
||||||
|
}
|
@@ -1,6 +1,7 @@
|
|||||||
import createAuthData from './create-auth-data';
|
import generateAuthUrl from './generate-auth-url';
|
||||||
import verifyCredentials from './verify-credentials';
|
import verifyCredentials from './verify-credentials';
|
||||||
import isStillVerified from './is-still-verified';
|
import isStillVerified from './is-still-verified';
|
||||||
|
import refreshToken from './refresh-token';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
fields: [
|
fields: [
|
||||||
@@ -17,8 +18,8 @@ export default {
|
|||||||
clickToCopy: true,
|
clickToCopy: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'consumerKey',
|
key: 'clientId',
|
||||||
label: 'Consumer Key',
|
label: 'Client ID',
|
||||||
type: 'string' as const,
|
type: 'string' as const,
|
||||||
required: true,
|
required: true,
|
||||||
readOnly: false,
|
readOnly: false,
|
||||||
@@ -28,8 +29,8 @@ export default {
|
|||||||
clickToCopy: false,
|
clickToCopy: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'consumerSecret',
|
key: 'clientSecret',
|
||||||
label: 'Consumer Secret',
|
label: 'Client Secret',
|
||||||
type: 'string' as const,
|
type: 'string' as const,
|
||||||
required: true,
|
required: true,
|
||||||
readOnly: false,
|
readOnly: false,
|
||||||
@@ -39,89 +40,9 @@ export default {
|
|||||||
clickToCopy: false,
|
clickToCopy: false,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
authenticationSteps: [
|
|
||||||
{
|
|
||||||
step: 1,
|
|
||||||
type: 'mutation' as const,
|
|
||||||
name: 'createConnection',
|
|
||||||
arguments: [
|
|
||||||
{
|
|
||||||
name: 'key',
|
|
||||||
value: '{key}',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'formattedData',
|
|
||||||
value: null,
|
|
||||||
properties: [
|
|
||||||
{
|
|
||||||
name: 'consumerKey',
|
|
||||||
value: '{fields.consumerKey}',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'consumerSecret',
|
|
||||||
value: '{fields.consumerSecret}',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
step: 2,
|
|
||||||
type: 'mutation' as const,
|
|
||||||
name: 'createAuthData',
|
|
||||||
arguments: [
|
|
||||||
{
|
|
||||||
name: 'id',
|
|
||||||
value: '{createConnection.id}',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
step: 3,
|
|
||||||
type: 'openWithPopup' as const,
|
|
||||||
name: 'openAuthPopup',
|
|
||||||
arguments: [
|
|
||||||
{
|
|
||||||
name: 'url',
|
|
||||||
value: '{createAuthData.url}',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
step: 4,
|
|
||||||
type: 'mutation' as const,
|
|
||||||
name: 'updateConnection',
|
|
||||||
arguments: [
|
|
||||||
{
|
|
||||||
name: 'id',
|
|
||||||
value: '{createConnection.id}',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'formattedData',
|
|
||||||
value: null,
|
|
||||||
properties: [
|
|
||||||
{
|
|
||||||
name: 'code',
|
|
||||||
value: '{openAuthPopup.code}',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
step: 5,
|
|
||||||
type: 'mutation' as const,
|
|
||||||
name: 'verifyConnection',
|
|
||||||
arguments: [
|
|
||||||
{
|
|
||||||
name: 'id',
|
|
||||||
value: '{createConnection.id}',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
|
|
||||||
createAuthData,
|
generateAuthUrl,
|
||||||
verifyCredentials,
|
verifyCredentials,
|
||||||
isStillVerified,
|
isStillVerified,
|
||||||
|
refreshToken,
|
||||||
};
|
};
|
||||||
|
@@ -2,12 +2,8 @@ import { IGlobalVariable } from '@automatisch/types';
|
|||||||
import getCurrentUser from '../common/get-current-user';
|
import getCurrentUser from '../common/get-current-user';
|
||||||
|
|
||||||
const isStillVerified = async ($: IGlobalVariable) => {
|
const isStillVerified = async ($: IGlobalVariable) => {
|
||||||
try {
|
|
||||||
const user = await getCurrentUser($);
|
const user = await getCurrentUser($);
|
||||||
return !!user;
|
return !!user;
|
||||||
} catch (error) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default isStillVerified;
|
export default isStillVerified;
|
||||||
|
26
packages/backend/src/apps/strava/auth/refresh-token.ts
Normal file
26
packages/backend/src/apps/strava/auth/refresh-token.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import { IGlobalVariable } from '@automatisch/types';
|
||||||
|
|
||||||
|
const refreshToken = async ($: IGlobalVariable) => {
|
||||||
|
const params = {
|
||||||
|
client_id: $.auth.data.clientId as string,
|
||||||
|
client_secret: $.auth.data.clientSecret as string,
|
||||||
|
grant_type: 'refresh_token',
|
||||||
|
refresh_token: $.auth.data.refreshToken as string,
|
||||||
|
};
|
||||||
|
|
||||||
|
const { data } = await $.http.post(
|
||||||
|
'/v3/oauth/token',
|
||||||
|
null,
|
||||||
|
{ params }
|
||||||
|
);
|
||||||
|
|
||||||
|
await $.auth.set({
|
||||||
|
accessToken: data.access_token,
|
||||||
|
expiresIn: data.expires_in,
|
||||||
|
expiresAt: data.expires_at,
|
||||||
|
tokenType: data.token_type,
|
||||||
|
refreshToken: data.refresh_token,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export default refreshToken;
|
@@ -1,28 +1,25 @@
|
|||||||
import qs from 'qs';
|
|
||||||
import { IGlobalVariable } from '@automatisch/types';
|
import { IGlobalVariable } from '@automatisch/types';
|
||||||
|
|
||||||
const verifyCredentials = async ($: IGlobalVariable) => {
|
const verifyCredentials = async ($: IGlobalVariable) => {
|
||||||
try {
|
const params = {
|
||||||
const searchParams = {
|
client_id: $.auth.data.clientId as string,
|
||||||
client_id: $.auth.data.consumerKey,
|
client_secret: $.auth.data.clientSecret as string,
|
||||||
client_secret: $.auth.data.consumerSecret,
|
code: $.auth.data.code as string,
|
||||||
code: $.auth.data.code,
|
|
||||||
grant_type: 'authorization_code',
|
grant_type: 'authorization_code',
|
||||||
};
|
};
|
||||||
const { data } = await $.http.post(
|
const { data } = await $.http.post(
|
||||||
`/v3/oauth/token?${qs.stringify(searchParams)}a`,
|
'/v3/oauth/token',
|
||||||
|
null,
|
||||||
|
{ params }
|
||||||
);
|
);
|
||||||
|
|
||||||
await $.auth.set({
|
await $.auth.set({
|
||||||
accessToken: data.access_token,
|
accessToken: data.access_token,
|
||||||
refreshToken: data.refresh_token,
|
refreshToken: data.refresh_token,
|
||||||
tokenType: data.token_type,
|
tokenType: data.token_type,
|
||||||
userId: data.athlete.id,
|
athleteId: data.athlete.id,
|
||||||
screenName: `${data.athlete.firstname} ${data.athlete.lastname}`,
|
screenName: `${data.athlete.firstname} ${data.athlete.lastname}`,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
throw new Error('Error occured while verifying credentials!');
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default verifyCredentials;
|
export default verifyCredentials;
|
||||||
|
Reference in New Issue
Block a user