Skip to content

Commit 76788ed

Browse files
committed
Prepare the store and send sendEmailLogin for magic codes
1 parent 9ff0adb commit 76788ed

File tree

8 files changed

+57
-8
lines changed

8 files changed

+57
-8
lines changed

client/blocks/login/index.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import ContinueAsUser from './continue-as-user';
5757
import ErrorNotice from './error-notice';
5858
import LoginForm from './login-form';
5959
import { LoginHeader } from './login-header';
60+
import { shouldUseMagicCode } from './utils/should-use-magic-code';
6061

6162
import './style.scss';
6263

@@ -740,6 +741,7 @@ export default connect(
740741
redirectTo: stateProps.redirectTo,
741742
loginFormFlow: true,
742743
showGlobalNotices: false,
744+
...( shouldUseMagicCode( { isJetpack: ownProps.isJetpack } ) && { tokenType: 'code' } ),
743745
source: stateProps.isWooJPC ? 'woo-passwordless-jpc' + '-' + get( stateProps, 'from' ) : '',
744746
flow:
745747
( ownProps.isJetpack && 'jetpack' ) ||

client/blocks/login/login-form.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import isWooJPCFlow from 'calypso/state/selectors/is-woo-jpc-flow';
6969
import ErrorNotice from './error-notice';
7070
import SocialLoginForm from './social';
7171
import { isA4AReferralClient } from './utils/is-a4a-referral-for-client';
72+
import { shouldUseMagicCode } from './utils/should-use-magic-code';
7273

7374
import './login-form.scss';
7475

@@ -335,6 +336,7 @@ export class LoginForm extends Component {
335336
redirectTo: this.props.redirectTo,
336337
requestLoginEmailFormFlow: true,
337338
createAccount: true,
339+
...( shouldUseMagicCode( { isJetpack: this.props.isJetpack } ) && { tokenType: 'code' } ),
338340
flow: 'jetpack',
339341
} );
340342
}

client/state/auth/actions.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'calypso/state/data-layer/wpcom/auth/send-login-email';
1515
* @param {boolean} options.showGlobalNotices - if true, displays global notices to user about the email
1616
* @param {string} options.flow - name of the login flow
1717
* @param {boolean} options.createAccount - if true, instructs the API to create a WPCOM account associated with email
18+
* @param {'link'|'code'} options.tokenType - type of token to send in the email
1819
* @param {boolean} options.source - source of the login request (optional)
1920
* @returns {Object} action object
2021
*/
@@ -29,6 +30,7 @@ export const sendEmailLogin = (
2930
isMobileAppLogin = false,
3031
flow = null,
3132
createAccount = false,
33+
tokenType = 'link',
3234
source = '',
3335
}
3436
) => {
@@ -48,6 +50,7 @@ export const sendEmailLogin = (
4850
requestLoginEmailFormFlow,
4951
flow,
5052
createAccount,
53+
tokenType,
5154
source,
5255
};
5356
};

client/state/data-layer/wpcom/auth/send-login-email/index.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const sendLoginEmail = ( action ) => {
3333
flow,
3434
createAccount,
3535
source,
36+
tokenType,
3637
} = action;
3738
const noticeAction = showGlobalNotices
3839
? infoNotice( translate( 'Sending email' ), { duration: 4000 } )
@@ -73,6 +74,7 @@ export const sendLoginEmail = ( action ) => {
7374
...( flow && { flow } ),
7475
create_account: createAccount,
7576
tos: getToSAcceptancePayload(),
77+
...( tokenType && { token_type: tokenType } ),
7678
source,
7779
calypso_env:
7880
window?.location?.host === 'wordpress.com' ? 'production' : config( 'env_id' ),
@@ -83,16 +85,13 @@ export const sendLoginEmail = ( action ) => {
8385
];
8486
};
8587

86-
export const onSuccess = ( {
87-
email,
88-
showGlobalNotices,
89-
infoNoticeId = null,
90-
loginFormFlow,
91-
requestLoginEmailFormFlow,
92-
} ) => [
88+
export const onSuccess = (
89+
{ email, showGlobalNotices, infoNoticeId = null, loginFormFlow, requestLoginEmailFormFlow },
90+
response
91+
) => [
9392
...( loginFormFlow || requestLoginEmailFormFlow
9493
? [
95-
{ type: MAGIC_LOGIN_REQUEST_LOGIN_EMAIL_SUCCESS },
94+
{ type: MAGIC_LOGIN_REQUEST_LOGIN_EMAIL_SUCCESS, response },
9695
{ type: MAGIC_LOGIN_SHOW_CHECK_YOUR_EMAIL_PAGE, email },
9796
]
9897
: [] ),

client/state/login/magic-login/actions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export const fetchMagicLoginAuthenticate =
8686

8787
dispatch( {
8888
type: MAGIC_LOGIN_REQUEST_AUTH_SUCCESS,
89+
data: json.data,
8990
} );
9091
} )
9192
.catch( ( error ) => {

client/state/login/magic-login/reducer.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,30 @@ export const requestEmailSuccess = ( state = false, action ) => {
133133
return state;
134134
};
135135

136+
export const publicToken = ( state = null, action ) => {
137+
switch ( action.type ) {
138+
case MAGIC_LOGIN_REQUEST_LOGIN_EMAIL_SUCCESS:
139+
return action.response?.public_token || null;
140+
case MAGIC_LOGIN_RESET_REQUEST_FORM:
141+
return null;
142+
}
143+
144+
return state;
145+
};
146+
147+
export const authSuccessData = ( state = null, action ) => {
148+
switch ( action.type ) {
149+
case MAGIC_LOGIN_REQUEST_AUTH_SUCCESS:
150+
return action.data || null;
151+
case MAGIC_LOGIN_REQUEST_AUTH_ERROR:
152+
case MAGIC_LOGIN_REQUEST_AUTH_FETCH:
153+
case MAGIC_LOGIN_RESET_REQUEST_FORM:
154+
return null;
155+
}
156+
157+
return state;
158+
};
159+
136160
export default combineReducers( {
137161
isFetchingAuth,
138162
isFetchingEmail,
@@ -141,4 +165,6 @@ export default combineReducers( {
141165
requestEmailError,
142166
requestEmailSuccess,
143167
currentView,
168+
publicToken,
169+
authSuccessData,
144170
} );
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Returns the magic login authentication success data if it exists
3+
* @param {Object} state Global state tree
4+
* @returns {Object | null} The auth success data if it exists, null otherwise
5+
*/
6+
export default function getMagicLoginAuthSuccessData( state ) {
7+
return state.login.magicLogin.authSuccessData;
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Returns the public token for magic login if it exists
3+
* @param {Object} state Global state tree
4+
* @returns {string|null} The public token if it exists, null otherwise
5+
*/
6+
export default function getMagicLoginPublicToken( state ) {
7+
return state.login.magicLogin.publicToken;
8+
}

0 commit comments

Comments
 (0)