| | import config from '@automattic/calypso-config'; |
| | import page from '@automattic/calypso-router'; |
| | import { getUrlParts } from '@automattic/calypso-url'; |
| | import { isGravPoweredOAuth2Client } from 'calypso/lib/oauth2-clients'; |
| | import { DesktopLoginStart, DesktopLoginFinalize } from 'calypso/login/desktop-login'; |
| | import { SOCIAL_HANDOFF_CONNECT_ACCOUNT } from 'calypso/state/action-types'; |
| | import { isUserLoggedIn, getCurrentUserLocale } from 'calypso/state/current-user/selectors'; |
| | import { fetchOAuth2ClientData } from 'calypso/state/oauth2-clients/actions'; |
| | import { getOAuth2Client } from 'calypso/state/oauth2-clients/selectors'; |
| | import LoginContextProvider from './login-context'; |
| | import MagicLogin from './magic-login'; |
| | import HandleEmailedLinkForm from './magic-login/handle-emailed-link-form'; |
| | import HandleEmailedLinkFormJetpackConnect from './magic-login/handle-emailed-link-form-jetpack-connect'; |
| | import QrCodeLoginPage from './qr-code-login-page'; |
| | import WPLogin from './wp-login'; |
| |
|
| | const enhanceContextWithLogin = ( context ) => { |
| | const { |
| | params: { flow, isJetpack, socialService, twoFactorAuthType, action }, |
| | path, |
| | query, |
| | isServerSide, |
| | } = context; |
| |
|
| | |
| | if ( query?.email_address && query?.service && query?.access_token && query?.id_token ) { |
| | context.store.dispatch( { |
| | type: SOCIAL_HANDOFF_CONNECT_ACCOUNT, |
| | email: query.email_address, |
| | authInfo: { |
| | service: query.service, |
| | access_token: query.access_token, |
| | id_token: query.id_token, |
| | }, |
| | } ); |
| |
|
| | |
| | if ( ! isServerSide ) { |
| | const params = new URLSearchParams( new URL( window.location.href ).search ); |
| | params.delete( 'service' ); |
| | params.delete( 'access_token' ); |
| | params.delete( 'id_token' ); |
| | page.redirect( window.location.pathname + '?' + params.toString() ); |
| | } |
| | } |
| |
|
| | const previousHash = context.state || {}; |
| | const { client_id, user_email, user_name, id_token, state } = previousHash; |
| | const currentState = context.store.getState(); |
| | const socialServiceResponse = client_id |
| | ? { client_id, user_email, user_name, id_token, state } |
| | : null; |
| | const clientId = query?.client_id; |
| | const oauth2ClientId = query?.oauth2_client_id; |
| | const oauth2Client = getOAuth2Client( currentState, Number( clientId || oauth2ClientId ) ) || {}; |
| | const isGravPoweredClient = isGravPoweredOAuth2Client( oauth2Client ); |
| | const isJetpackLogin = isJetpack === 'jetpack'; |
| |
|
| | context.primary = ( |
| | <LoginContextProvider> |
| | <WPLogin |
| | action={ action } |
| | isJetpack={ isJetpackLogin } |
| | isGravPoweredClient={ isGravPoweredClient } |
| | path={ path } |
| | twoFactorAuthType={ twoFactorAuthType } |
| | socialService={ socialService } |
| | socialServiceResponse={ socialServiceResponse } |
| | socialConnect={ flow === 'social-connect' } |
| | domain={ ( query && query.domain ) || null } |
| | fromSite={ ( query && query.site ) || null } |
| | signupUrl={ ( query && query.signup_url ) || null } |
| | /> |
| | </LoginContextProvider> |
| | ); |
| | }; |
| |
|
| | export async function login( context, next ) { |
| | const { |
| | query: { client_id, redirect_to }, |
| | } = context; |
| |
|
| | |
| | if ( context.hash && context.hash.client_id ) { |
| | page.replace( context.path, context.hash ); |
| |
|
| | return; |
| | } |
| |
|
| | if ( client_id ) { |
| | if ( ! redirect_to ) { |
| | const error = new Error( 'The `redirect_to` query parameter is missing.' ); |
| | error.status = 401; |
| | return next( error ); |
| | } |
| |
|
| | const { searchParams: redirectParams } = getUrlParts( redirect_to ); |
| | const back = redirectParams.get( 'back' ); |
| |
|
| | const redirectClientId = |
| | redirectParams.get( 'client_id' ) || |
| | |
| | ( back ? getUrlParts( back ).searchParams.get( 'client_id' ) : null ); |
| |
|
| | if ( client_id !== redirectClientId ) { |
| | const error = new Error( |
| | 'The `redirect_to` query parameter is invalid with the given `client_id`.' |
| | ); |
| | error.status = 401; |
| | return next( error ); |
| | } |
| |
|
| | const OAuth2Client = getOAuth2Client( context.store.getState(), client_id ); |
| | if ( ! OAuth2Client ) { |
| | |
| | try { |
| | await context.store.dispatch( fetchOAuth2ClientData( client_id ) ); |
| | } catch ( error ) { |
| | return next( error ); |
| | } |
| | } |
| | } |
| |
|
| | enhanceContextWithLogin( context ); |
| |
|
| | next(); |
| | } |
| |
|
| | export function desktopLogin( context, next ) { |
| | context.primary = <DesktopLoginStart />; |
| | next(); |
| | } |
| |
|
| | export function desktopLoginFinalize( context, next ) { |
| | const { hash } = context; |
| | context.primary = ( |
| | <DesktopLoginFinalize error={ hash?.error } accessToken={ hash?.access_token } /> |
| | ); |
| | next(); |
| | } |
| |
|
| | export async function magicLogin( context, next ) { |
| | const { |
| | path, |
| | query: { gravatar_flow, client_id, redirect_to }, |
| | } = context; |
| |
|
| | if ( isUserLoggedIn( context.store.getState() ) ) { |
| | return login( context, next ); |
| | } |
| |
|
| | |
| | if ( gravatar_flow ) { |
| | if ( ! client_id ) { |
| | const error = new Error( 'The `client_id` query parameter is missing.' ); |
| | error.status = 401; |
| | return next( error ); |
| | } |
| |
|
| | if ( ! redirect_to ) { |
| | const error = new Error( 'The `redirect_to` query parameter is missing.' ); |
| | error.status = 401; |
| | return next( error ); |
| | } |
| |
|
| | const oauth2Client = getOAuth2Client( context.store.getState(), client_id ); |
| | |
| | if ( ! oauth2Client ) { |
| | try { |
| | await context.store.dispatch( fetchOAuth2ClientData( client_id ) ); |
| | } catch ( error ) { |
| | return next( error ); |
| | } |
| | } |
| | } |
| |
|
| | context.primary = ( |
| | <LoginContextProvider> |
| | <MagicLogin path={ path } /> |
| | </LoginContextProvider> |
| | ); |
| |
|
| | next(); |
| | } |
| |
|
| | export function qrCodeLogin( context, next ) { |
| | const { redirect_to } = context.query; |
| |
|
| | |
| | const isJetpack = context.path.includes( '/jetpack' ); |
| |
|
| | context.primary = ( |
| | <QrCodeLoginPage |
| | locale={ context.params.lang } |
| | redirectTo={ redirect_to } |
| | isJetpack={ isJetpack } |
| | /> |
| | ); |
| |
|
| | next(); |
| | } |
| |
|
| | function getHandleEmailedLinkFormComponent( flow ) { |
| | if ( flow === 'jetpack' ) { |
| | return HandleEmailedLinkFormJetpackConnect; |
| | } |
| | return HandleEmailedLinkForm; |
| | } |
| |
|
| | export function magicLoginUse( context, next ) { |
| | |
| | |
| | |
| | |
| | if ( context.querystring ) { |
| | page.replace( context.pathname, context.query ); |
| |
|
| | return; |
| | } |
| |
|
| | const previousQuery = context.state || {}; |
| |
|
| | const { client_id, email, redirect_to, path, token, transition: isTransition } = previousQuery; |
| |
|
| | let activate = ''; |
| | try { |
| | const params = new URLSearchParams( new URL( redirect_to ).search ); |
| | activate = params.get( 'activate' ); |
| | } catch ( e ) { |
| | |
| | } |
| | const transition = isTransition === 'true'; |
| |
|
| | const flow = |
| | redirect_to?.includes( 'jetpack/connect' ) || path?.includes( 'jetpack/link/use' ) |
| | ? 'jetpack' |
| | : null; |
| |
|
| | const PrimaryComponent = getHandleEmailedLinkFormComponent( flow ); |
| |
|
| | const isJetpack = context.path.includes( '/jetpack' ); |
| |
|
| | context.primary = ( |
| | <PrimaryComponent |
| | clientId={ client_id } |
| | emailAddress={ email } |
| | token={ token } |
| | redirectTo={ redirect_to } |
| | transition={ transition } |
| | activate={ activate } |
| | isJetpack={ isJetpack } |
| | /> |
| | ); |
| |
|
| | next(); |
| | } |
| |
|
| | export function redirectDefaultLocale( context, next ) { |
| | |
| | if ( context.isServerSide ) { |
| | return next(); |
| | } |
| | |
| | if ( context.pathname !== '/log-in/en' && context.pathname !== '/log-in/jetpack/en' ) { |
| | if ( ! isUserLoggedIn( context.store.getState() ) && ! context.params.lang ) { |
| | context.params.lang = config( 'i18n_default_locale_slug' ); |
| | } |
| | return next(); |
| | } |
| |
|
| | |
| | if ( |
| | ! isUserLoggedIn( context.store.getState() ) && |
| | ! config.isEnabled( 'wpcom-user-bootstrap' ) |
| | ) { |
| | return next(); |
| | } |
| |
|
| | |
| | |
| | const currentUserLocale = getCurrentUserLocale( context.store.getState() ); |
| | if ( currentUserLocale && currentUserLocale !== 'en' ) { |
| | return next(); |
| | } |
| |
|
| | if ( context.params.isJetpack === 'jetpack' ) { |
| | page.redirect( '/log-in/jetpack' ); |
| | } else { |
| | page.redirect( '/log-in' ); |
| | } |
| | } |
| |
|
| | export function redirectJetpack( context, next ) { |
| | const { isJetpack } = context.params; |
| | const { redirect_to } = context.query; |
| |
|
| | const isUserComingFromPricingPage = |
| | redirect_to?.includes( 'source=jetpack-plans' ) || |
| | redirect_to?.includes( 'source=jetpack-connect-plans' ); |
| | const isUserComingFromMigrationPlugin = redirect_to?.includes( 'wpcom-migration' ); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | const pathAlreadyUpdated = context.path.includes( 'log-in/jetpack' ); |
| | if ( pathAlreadyUpdated ) { |
| | next(); |
| | return; |
| | } |
| |
|
| | if ( |
| | ( isJetpack !== 'jetpack' && |
| | redirect_to?.includes( 'jetpack/connect' ) && |
| | ! isUserComingFromMigrationPlugin ) || |
| | isUserComingFromPricingPage |
| | ) { |
| | return context.redirect( context.path.replace( 'log-in', 'log-in/jetpack' ) ); |
| | } |
| | next(); |
| | } |
| |
|