-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] - Spike of what authenticating flow requests might look like
- Loading branch information
1 parent
34199f6
commit fe0ffdf
Showing
3 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/shopify-app-remix/src/server/authenticate/flow/authenticate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import {adminClientFactory} from 'src/server/clients/admin'; | ||
import {BasicParams} from 'src/server/types'; | ||
|
||
export function authenticateFlowFactory(params: BasicParams) { | ||
const {api, config, logger} = params; | ||
|
||
return async function authenticate(request: Request) { | ||
logger.info('Authenticating flow request'); | ||
|
||
if (request.method !== 'POST') { | ||
logger.debug( | ||
'Received a non-POST request for flow. Only POST requests are allowed.', | ||
{url: request.url, method: request.method}, | ||
); | ||
throw new Response(undefined, { | ||
status: 405, | ||
statusText: 'Method not allowed', | ||
}); | ||
} | ||
|
||
const rawBody = await request.text(); | ||
const {valid} = await api.flow.validate({ | ||
rawBody, | ||
rawRequest: request, | ||
}); | ||
|
||
if (!valid) { | ||
throw new Response(undefined, { | ||
status: 400, | ||
statusText: 'Bad Request', | ||
}); | ||
} | ||
|
||
const payload = JSON.parse(rawBody); | ||
const sessionId = api.session.getOfflineId(payload.shopify_domain); | ||
const session = await config.sessionStorage.loadSession(sessionId); | ||
|
||
if (!session) { | ||
logger.info('Flow request could not find session', { | ||
shop: payload.shopify_domain, | ||
}); | ||
throw new Response(undefined, { | ||
status: 400, | ||
statusText: 'Bad Request', | ||
}); | ||
} | ||
|
||
return { | ||
session, | ||
payload, | ||
admin: adminClientFactory({params, session}), | ||
}; | ||
}; | ||
} |
Empty file.