Skip to content

Commit

Permalink
feat(amplify_auth_cognito): resendSignUpCode (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
haverchuck committed Aug 12, 2020
1 parent 3fb4e1d commit 5e9509e
Show file tree
Hide file tree
Showing 21 changed files with 421 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public class AuthCognito : FlutterPlugin, ActivityAware, MethodCallHandler {
"resetPassword" -> onResetPassword(result, data)
"confirmPassword" -> onConfirmPassword(result, data)
"fetchAuthSession" -> onFetchAuthSession(result, data)
"resendSignUpCode" -> onResendSignUpCode(result, data)
else -> result.notImplemented()
}
}
Expand Down Expand Up @@ -215,6 +216,23 @@ public class AuthCognito : FlutterPlugin, ActivityAware, MethodCallHandler {
}
}

private fun onResendSignUpCode(@NonNull flutterResult: Result, @NonNull request: HashMap<String, *>){
if (FlutterResendSignUpCodeRequest.validate(request)) {
var req = FlutterResendSignUpCodeRequest(request as HashMap<String, *>);
try {
Amplify.Auth.resendSignUpCode(
req.username,
{ result -> this.mainActivity?.runOnUiThread({ prepareSignUpResult(flutterResult, result) }) },
{ error -> this.mainActivity?.runOnUiThread({ prepareError(flutterResult, error, FlutterAuthFailureMessage.RESEND_SIGNUP_CODE.toString()) }) }
)
} catch (e: Exception) {
prepareError(flutterResult, e, FlutterAuthFailureMessage.RESEND_SIGNUP_CODE.toString())
}
} else {
prepareError(flutterResult, java.lang.Exception(FlutterAuthFailureMessage.MALFORMED.toString()), FlutterAuthFailureMessage.MALFORMED.toString())
}
}

private fun onSignIn (@NonNull flutterResult: Result, @NonNull request: HashMap<String, *>) {
if (FlutterSignInRequest.validate(request)) {
var req = FlutterSignInRequest(request)
Expand All @@ -229,7 +247,7 @@ public class AuthCognito : FlutterPlugin, ActivityAware, MethodCallHandler {
prepareError(flutterResult, e, FlutterAuthFailureMessage.SIGNIN.toString())
}
} else {
prepareError(flutterResult, java.lang.Exception("Amplify SignIn request is malformed"), FlutterAuthFailureMessage.MALFORMED.toString())
prepareError(flutterResult, java.lang.Exception(FlutterAuthFailureMessage.MALFORMED.toString()), FlutterAuthFailureMessage.MALFORMED.toString())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ enum class FlutterAuthFailureMessage {
return "AMPLIFY_CONFIRM_SIGNUP_FAILED"
}
},
RESEND_SIGNUP_CODE {
override fun toString(): String {
return "AMPLIFY_RESEND_SIGNUP_CODE_FAILED"
}
},
SIGNIN {
override fun toString(): String {
return "AMPLIFY_SIGNIN_FAILED"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amazonaws.amplify.amplify_auth_cognito.types

data class FlutterResendSignUpCodeRequest(val map: HashMap<String, *>) {
val username: String = map["username"] as String;
val options: HashMap<String, *>? = map["options"] as HashMap<String, *>?;

companion object {
fun validate(req : HashMap<String, *>?): Boolean {
var valid: Boolean = true;
if (req != null) {
if (!req.containsKey("username") && req["username"] != "") {
valid = false;
}
}
return valid;
}
}
}
29 changes: 28 additions & 1 deletion packages/amplify_auth_cognito/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* permissions and limitations under the License.
*/

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_core/amplify_core.dart';
Expand Down Expand Up @@ -279,6 +278,29 @@ class _MyAppState extends State<MyApp> {
}
}

void _resendSignUpCode() async {
setState(() {
error = "";
exceptions = [];
});
try {
ResendSignUpCodeResult res = await Amplify.Auth.resendSignUpCode(
request: ResendSignUpCodeRequest(
username: usernameController.text.trim(),
),
);
print(res);
} on AuthError catch (e) {
setState(() {
error = e.cause;
e.exceptionList.forEach((el) {
exceptions.add(el.exception);
});
});
print(e);
}
}

void _confirmReset() async {
setState(() {
error = "";
Expand Down Expand Up @@ -385,6 +407,11 @@ Widget showConfirmSignUp() {
onPressed:_backToSignIn,
child: const Text('Back to Sign In'),
),
const Padding(padding: EdgeInsets.all(10.0)),
RaisedButton(
onPressed:_resendSignUpCode,
child: const Text('ResendCode'),
),
],
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum FlutterAuthErrorMessage: String {
case SIGNUP = "AMPLIFY_SIGNUP_FAILED"
case UPDATE_PASSWORD = "AMPLIFY_UPDATE_PASSWORD_FAILED"
case RESET_PASSWORD = "AMPLIFY_RESET_PASSWORD_FAILED"
case RESEND_SIGNUP = "AMPLIFY_RESEND_SIGNUP_CODE_FAILED"
case CONFIRM_PASSWORD = "AMPLIFY_CONFIRM_PASSWORD_FAILED"
case FETCH_SESSION = "AMPLIFY_FETCH_SESSION_FAILED"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import Foundation
struct FlutterResendSignUpCodeRequest {
var username: String
init(dict: NSMutableDictionary){
self.username = dict["username"] as! String
}
static func validate(dict: NSMutableDictionary) -> Bool {
var valid: Bool = true;
if (dict["username"] == nil) {
valid = false;
}
return valid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import Flutter
import Foundation
import Amplify

struct FlutterResendSignUpCodeResult {
var codeDeliveryDetails: [String: String]

init(res: AmplifyOperation<AuthResendSignUpCodeRequest, AuthCodeDeliveryDetails, AuthError>.OperationResult){
self.codeDeliveryDetails = setCodeDeliveryDetails(res: res)
}

func toJSON() -> Dictionary<String, Any> {
return ["codeDeliveryDetails": self.codeDeliveryDetails]
}
}


func setCodeDeliveryDetails(res: AmplifyOperation<AuthResendSignUpCodeRequest, AuthCodeDeliveryDetails, AuthError>.OperationResult) -> [String: String] {
var deliveryMap: [String: String] = [:]
switch res {
case .success(let codeDeliveryDetails):
if case let .email(e) = codeDeliveryDetails.destination {
deliveryMap["destination"] = e! as String
deliveryMap["attributeName"] = "email"
deliveryMap["deliveryMedium"] = "EMAIL"
}

if case let .phone(e) = codeDeliveryDetails.destination {
deliveryMap["destination"] = e! as String
deliveryMap["attributeName"] = "phone"
}

if case let .sms(e) = codeDeliveryDetails.destination {
deliveryMap["destination"] = e! as String
deliveryMap["attributeName"] = "sms"
deliveryMap["deliveryMedium"] = "SMS"
}

if case let .unknown(e) = codeDeliveryDetails.destination {
deliveryMap["destination"] = e! as String
deliveryMap["attributeName"] = "unknown"
}
case .failure:
deliveryMap = [:]
}
return deliveryMap
}
34 changes: 17 additions & 17 deletions packages/amplify_auth_cognito/ios/Classes/FlutterSignUpResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,26 @@ func setCodeDeliveryDetails(res: AmplifyOperation<AuthSignUpRequest, AuthSignUpR
case .success(let signUpResult):
if case let .confirmUser(deliveryDetails, _) = signUpResult.nextStep {
if case let .email(e) = deliveryDetails?.destination {
deliveryMap["destination"] = e! as String
deliveryMap["attributeName"] = "email"
deliveryMap["deliveryMedium"] = "EMAIL"
}
deliveryMap["destination"] = e! as String
deliveryMap["attributeName"] = "email"
deliveryMap["deliveryMedium"] = "EMAIL"
}

if case let .phone(e) = deliveryDetails?.destination {
deliveryMap["destination"] = e! as String
deliveryMap["attributeName"] = "phone"
}
if case let .phone(e) = deliveryDetails?.destination {
deliveryMap["destination"] = e! as String
deliveryMap["attributeName"] = "phone"
}

if case let .sms(e) = deliveryDetails?.destination {
deliveryMap["destination"] = e! as String
deliveryMap["attributeName"] = "sms"
deliveryMap["deliveryMedium"] = "SMS"
}
if case let .sms(e) = deliveryDetails?.destination {
deliveryMap["destination"] = e! as String
deliveryMap["attributeName"] = "sms"
deliveryMap["deliveryMedium"] = "SMS"
}

if case let .unknown(e) = deliveryDetails?.destination {
deliveryMap["destination"] = e! as String
deliveryMap["attributeName"] = "unknown"
}
if case let .unknown(e) = deliveryDetails?.destination {
deliveryMap["destination"] = e! as String
deliveryMap["attributeName"] = "unknown"
}
}
case .failure:
deliveryMap = [:]
Expand Down
39 changes: 30 additions & 9 deletions packages/amplify_auth_cognito/ios/Classes/SwiftAuthCognito.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,31 @@ public class SwiftAuthCognito: NSObject, FlutterPlugin, FlutterStreamHandler {
}
case "confirmSignUp":
if (FlutterConfirmSignUpRequest.validate(dict: data)) {
let request = FlutterConfirmSignUpRequest(dict: data)
let request = FlutterConfirmSignUpRequest(dict: data)
onConfirmSignUp(flutterResult: result, request: request)
} else {
let errorCode = "UNKNOWN"
self.prepareError(flutterResult: result, msg: FlutterAuthErrorMessage.MALFORMED.rawValue, errorMap: self.formatErrorMap(errorCode: errorCode))
}
case "resendSignUpCode":
if (FlutterResendSignUpCodeRequest.validate(dict: data)) {
let request = FlutterResendSignUpCodeRequest(dict: data)
onResendSignUpCode(flutterResult: result, request: request)
} else {
let errorCode = "UNKNOWN"
self.prepareError(flutterResult: result, msg: FlutterAuthErrorMessage.MALFORMED.rawValue, errorMap: self.formatErrorMap(errorCode: errorCode))
}
case "signIn":
if (FlutterSignInRequest.validate(dict: data)) {
let request = FlutterSignInRequest(dict: data)
let request = FlutterSignInRequest(dict: data)
onSignIn(flutterResult: result, request: request)
} else {
let errorCode = "UNKNOWN"
self.prepareError(flutterResult: result, msg: FlutterAuthErrorMessage.MALFORMED.rawValue, errorMap: self.formatErrorMap(errorCode: errorCode))
}
case "confirmSignIn":
if (FlutterConfirmSignInRequest.validate(dict: data)) {
let request = FlutterConfirmSignInRequest(dict: data)
let request = FlutterConfirmSignInRequest(dict: data)
onConfirmSignIn(flutterResult: result, request: request)
} else {
let errorCode = "UNKNOWN"
Expand Down Expand Up @@ -178,18 +186,31 @@ public class SwiftAuthCognito: NSObject, FlutterPlugin, FlutterStreamHandler {
}
}
}


private func onSignIn(flutterResult: @escaping FlutterResult, request: FlutterSignInRequest) {
_ = Amplify.Auth.signIn(username: request.username, password:request.password) { response in
switch response {
private func onResendSignUpCode(flutterResult: @escaping FlutterResult, request: FlutterResendSignUpCodeRequest) {
_ = Amplify.Auth.resendSignUpCode(for: request.username) { response in
switch response {
case .success:
let signInData = FlutterSignInResult(res: response)
flutterResult(signInData.toJSON())
let resendData = FlutterResendSignUpCodeResult(res: response)
flutterResult(resendData.toJSON())
case .failure(let signUpError):
self.handleAuthError(error: signUpError, flutterResult: flutterResult, msg: FlutterAuthErrorMessage.RESEND_SIGNUP.rawValue)
}
}
}


private func onSignIn(flutterResult: @escaping FlutterResult, request: FlutterSignInRequest) {
_ = Amplify.Auth.signIn(username: request.username, password:request.password) { response in
switch response {
case .success:
let signInData = FlutterSignInResult(res: response)
flutterResult(signInData.toJSON())
case .failure(let signInError):
self.handleAuthError(error: signInError, flutterResult: flutterResult, msg: FlutterAuthErrorMessage.SIGNIN.rawValue)
}
}
}
}

private func onConfirmSignIn(flutterResult: @escaping FlutterResult, request: FlutterConfirmSignInRequest) {
Expand Down
5 changes: 5 additions & 0 deletions packages/amplify_auth_cognito/lib/amplify_auth_cognito.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class AmplifyAuthCognito extends AuthPluginInterface {
return res;
}

Future<ResendSignUpCodeResult> resendSignUpCode({@required ResendSignUpCodeRequest request}) async {
var res = await _instance.resendSignUpCode(request: request);
return res;
}

Future<SignInResult> signIn({@required SignInRequest request}) async {
var res = await _instance.signIn(request: request);
return res;
Expand Down
Loading

0 comments on commit 5e9509e

Please sign in to comment.