[Huawei Developers] Basavaraj.navi:
Introduction

AppGallery Connect provides a cloud-based auth service and SDKs to help you quickly build a secure and reliable user authentication system for your apps to verify user identity. The AppGallery Connect auth service supports multiple authentication methods and is seamlessly integrated with other Serverless services to help you secure user data based on simple rules that you have defined.

In this article, we will cover just the mobile number authentication method in Flutter.

Auth Service supported accounts

Phone

Huawei ID

Huawei Game Service

WeChat

Weibo

QQ

Email

Integration of Crash service

1. Configure application on the AGC

2. Client application development process

Configure application on the AGC

This step involves a couple of steps, as follows.

Step 1: We need to register as a developer account in AppGallery Connect. If you are already a developer ignore this step.

Step 2: Create an app by referring to Creating a Project and Creating an App in the Project

Step 3: Set the data storage location based on the current location.

Step 4: Enabling Crash Kit. Open AppGallery connect, choose project settings > Build> Auth Service

Step 5: Generating a Signing Certificate Fingerprint.

Step 6: Configuring the Signing Certificate Fingerprint.

Step 7: Download your agconnect-services.json file, paste it into the app root directory.

Client application development process

This step involves the couple of steps as follows.

Step 1: Create flutter application in the Android studio (Any IDE which is your favorite).

Step 2: Add the App level gradle dependencies. Choose inside project Android > app > build.gradle

1

2	apply plugin: 'com.android.application'

apply plugin: 'com.huawei.agconnect'

Root level gradle dependencies

1

2	maven { url 'https://developer.huawei.com/repo/' }

classpath 'com.huawei.agconnect:agcp:1.4.1.300'

Add the below permissions in Android Manifest file.

1	<uses-permission android:name="android.permission.INTERNET" />

Step 3: Add the agconnect_auth in pubspec.yaml

Step 4: Add downloaded file into outside project directory. Declare plugin path in pubspec.yaml file under dependencies.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25	name: tic_tac_toe

description: A new Flutter project.

version: 1.0.0+1

environment:

sdk: ">=2.1.0 <3.0.0"

dependencies:

flutter:

sdk: flutter

cupertino_icons: ^0.1.2

agconnect_auth: ^1.1.0

dev_dependencies:

flutter_test:

sdk: flutter

flutter:

uses-material-design: true

assets:

- images/cross.png

- images/circle.png

- images/edit.png

- images/delete.png

- images/success.png

- images/otp-icon.png

To achieve Auth service example let’s follow the steps

1. AGC Configuration

2. Build Flutter application

Step 1: AGC Configuration

1. Sign in to AppGallery Connect and select My apps.

2. Select the app in which you want to integrate Crash Service.

3. Navigate to Project Setting > Build> Auth Service

Step 2: Build Flutter application

homepage.dart

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131	import 'package:agconnect_auth/agconnect_auth.dart';

import 'package:flutter/material.dart';

import 'package:tic_tac_toe/Constant/Constant.dart';

class HomePage extends StatelessWidget {

@override

Widget build(BuildContext context) {

List<Widget> widgetList = [];

return Scaffold(

appBar: AppBar(

backgroundColor: Colors.red,

title: Text(

"Login",

style: TextStyle(

color: Colors.white,

fontWeight: FontWeight.bold,

),

),

),

backgroundColor: Color(0xFFeaeaea),

body: ListView(

shrinkWrap: true,

scrollDirection: Axis.vertical,

children: <Widget>[

Column(

mainAxisSize: MainAxisSize.max,

mainAxisAlignment: MainAxisAlignment.start,

crossAxisAlignment: CrossAxisAlignment.center,

children: <Widget>[

Padding(

padding: const EdgeInsets.only(

left: 16.0, top: 20.0, right: 16.0),

child: Text(

"Enter your phone number",

style: TextStyle(

fontSize: 18.0,

fontWeight: FontWeight.bold,

color: Colors.black),

textAlign: TextAlign.center,

),

),

Padding(

padding: const EdgeInsets.only(top: 30.0),

child: Image(

image: AssetImage('images/otp-icon.png'),

height: 120.0,

width: 120.0,

),

),

Row(

children: <Widget>[

Flexible(

child: new Container(),

flex: 1,

),

Flexible(

child: new TextFormField(

textAlign: TextAlign.center,

autofocus: false,

enabled: false,

initialValue: "+91",

style: TextStyle(fontSize: 20.0, color: Colors.black),

),

flex: 3,

),

Flexible(

child: new Container(),

flex: 1,

),

Flexible(

child: new TextFormField(

textAlign: TextAlign.start,

autofocus: false,

enabled: true,

keyboardType: TextInputType.number,

textInputAction: TextInputAction.done,

style: TextStyle(fontSize: 20.0, color: Colors.black),

),

flex: 9,

),

Flexible(

child: new Container(),

flex: 1,

),

],

),

Padding(

padding: const EdgeInsets.only(top: 40.0, bottom: 40.0),

child: new Container(

width: 150.0,

height: 40.0,

child: new RaisedButton(

onPressed: () {

//Navigator.of(context).pushNamed(OTP_SCREEN);

checkIsUserSignedIn(context);

},

child: Text("Get OTP"),

textColor: Colors.white,

color: Colors.red,

shape: new RoundedRectangleBorder(

borderRadius: new BorderRadius.circular(30.0))),

),

)

])

],

));

}

void checkIsUserSignedIn(BuildContext context) {

AGCAuth.instance.currentUser.then((user) {

if (user != null) {

print("User is already signed in");

} else {

requestCode(context, '91', '9731276143');

}

});

}

void requestCode(

BuildContext context, String countryCode, String phoneNumber) {

VerifyCodeSettings settings =

VerifyCodeSettings(VerifyCodeAction.registerLogin, sendInterval: 30);

PhoneAuthProvider.requestVerifyCode(countryCode, phoneNumber, settings)

.then((result) {

print("Requested verification code");

Navigator.of(context).pushNamed(OTP_SCREEN);

});

}

}

otppage.dart

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533	import 'package:agconnect_auth/agconnect_auth.dart';

import 'package:flutter/material.dart';

import 'package:flutter/services.dart';

class OtpPage extends StatefulWidget {

@override

OtpPageState createState() => OtpPageState();

}

class OtpPageState extends State<OtpPage> {

TextEditingController controller1 = new TextEditingController();

TextEditingController controller2 = new TextEditingController();

TextEditingController controller3 = new TextEditingController();

TextEditingController controller4 = new TextEditingController();

TextEditingController controller5 = new TextEditingController();

TextEditingController controller6 = new TextEditingController();

TextEditingController currController = new TextEditingController();

@override

void dispose() {

super.dispose();

controller1.dispose();

controller2.dispose();

controller3.dispose();

controller4.dispose();

controller5.dispose();

controller6.dispose();

}

@override

void initState() {

// TODO: implement initState

super.initState();

currController = controller1;

}

@override

Widget build(BuildContext context) {

List<Widget> widgetList = [

Padding(

padding: EdgeInsets.only(left: 0.0, right: 2.0),

child: new Container(

color: Colors.transparent,

),

),

Padding(

padding: const EdgeInsets.only(right: 2.0, left: 2.0),

child: new Container(

alignment: Alignment.center,

decoration: new BoxDecoration(

color: Color.fromRGBO(0, 0, 0, 0.1),

border: new Border.all(

width: 1.0, color: Color.fromRGBO(0, 0, 0, 0.1)),

borderRadius: new BorderRadius.circular(4.0)),

child: new TextField(

inputFormatters: [

LengthLimitingTextInputFormatter(1),

],

enabled: false,

controller: controller1,

autofocus: false,

textAlign: TextAlign.center,

style: TextStyle(fontSize: 24.0, color: Colors.black),

)),

),

Padding(

padding: const EdgeInsets.only(right: 2.0, left: 2.0),

child: new Container(

alignment: Alignment.center,

decoration: new BoxDecoration(

color: Color.fromRGBO(0, 0, 0, 0.1),

border: new Border.all(

width: 1.0, color: Color.fromRGBO(0, 0, 0, 0.1)),

borderRadius: new BorderRadius.circular(4.0)),

child: new TextField(

inputFormatters: [

LengthLimitingTextInputFormatter(1),

],

controller: controller2,

autofocus: false,

enabled: false,

keyboardType: TextInputType.number,

textAlign: TextAlign.center,

style: TextStyle(fontSize: 24.0, color: Colors.black),

),

),

),

Padding(

padding: const EdgeInsets.only(right: 2.0, left: 2.0),

child: new Container(

alignment: Alignment.center,

decoration: new BoxDecoration(

color: Color.fromRGBO(0, 0, 0, 0.1),

border: new Border.all(

width: 1.0, color: Color.fromRGBO(0, 0, 0, 0.1)),

borderRadius: new BorderRadius.circular(4.0)),

child: new TextField(

inputFormatters: [

LengthLimitingTextInputFormatter(1),

],

keyboardType: TextInputType.number,

controller: controller3,

textAlign: TextAlign.center,

autofocus: false,

enabled: false,

style: TextStyle(fontSize: 24.0, color: Colors.black),

),

),

),

Padding(

padding: const EdgeInsets.only(right: 2.0, left: 2.0),

child: new Container(

alignment: Alignment.center,

decoration: new BoxDecoration(

color: Color.fromRGBO(0, 0, 0, 0.1),

border: new Border.all(

width: 1.0, color: Color.fromRGBO(0, 0, 0, 0.1)),

borderRadius: new BorderRadius.circular(4.0)),

child: new TextField(

inputFormatters: [

LengthLimitingTextInputFormatter(1),

],

textAlign: TextAlign.center,

controller: controller4,

autofocus: false,

enabled: false,

style: TextStyle(fontSize: 24.0, color: Colors.black),

),

),

),

Padding(

padding: const EdgeInsets.only(right: 2.0, left: 2.0),

child: new Container(

alignment: Alignment.center,

decoration: new BoxDecoration(

color: Color.fromRGBO(0, 0, 0, 0.1),

border: new Border.all(

width: 1.0, color: Color.fromRGBO(0, 0, 0, 0.1)),

borderRadius: new BorderRadius.circular(4.0)),

child: new TextField(

inputFormatters: [

LengthLimitingTextInputFormatter(1),

],

textAlign: TextAlign.center,

controller: controller5,

autofocus: false,

enabled: false,

style: TextStyle(fontSize: 24.0, color: Colors.black),

),

),

),

Padding(

padding: const EdgeInsets.only(right: 2.0, left: 2.0),

child: new Container(

alignment: Alignment.center,

decoration: new BoxDecoration(

color: Color.fromRGBO(0, 0, 0, 0.1),

border: new Border.all(

width: 1.0, color: Color.fromRGBO(0, 0, 0, 0.1)),

borderRadius: new BorderRadius.circular(4.0)),

child: new TextField(

inputFormatters: [

LengthLimitingTextInputFormatter(1),

],

textAlign: TextAlign.center,

controller: controller6,

autofocus: false,

enabled: false,

style: TextStyle(fontSize: 24.0, color: Colors.black),

),

),

),

Padding(

padding: EdgeInsets.only(left: 2.0, right: 0.0),

child: new Container(

color: Colors.transparent,

),

),

];

return new Scaffold(

resizeToAvoidBottomInset: false,

appBar: AppBar(

title: Text("Enter OTP"),

backgroundColor: Colors.red,

),

backgroundColor: Color(0xFFeaeaea),

body: Container(

child: Column(

children: <Widget>[

Flexible(

child: Column(

mainAxisSize: MainAxisSize.max,

mainAxisAlignment: MainAxisAlignment.start,

children: <Widget>[

Padding(

padding: const EdgeInsets.only(top: 8.0),

child: Text(

"Verifying your number!",

style: TextStyle(

fontSize: 18.0, fontWeight: FontWeight.bold),

),

),

Padding(

padding: const EdgeInsets.only(

left: 16.0, top: 4.0, right: 16.0),

child: Text(

"Please type the verification code sent to",

style: TextStyle(

fontSize: 15.0, fontWeight: FontWeight.normal),

textAlign: TextAlign.center,

),

),

Padding(

padding: const EdgeInsets.only(

left: 30.0, top: 2.0, right: 30.0),

child: Text(

"+91 9731276143",

style: TextStyle(

fontSize: 15.0,

fontWeight: FontWeight.bold,

color: Colors.red),

textAlign: TextAlign.center,

),

),

Padding(

padding: const EdgeInsets.only(top: 16.0),

child: Image(

image: AssetImage('images/otp-icon.png'),

height: 120.0,

width: 120.0,

),

)

],

),

flex: 90,

),

Flexible(

child: Column(

mainAxisSize: MainAxisSize.max,

mainAxisAlignment: MainAxisAlignment.start,

children: <Widget>[

GridView.count(

crossAxisCount: 8,

mainAxisSpacing: 10.0,

shrinkWrap: true,

primary: false,

scrollDirection: Axis.vertical,

children: List<Container>.generate(

8,

(int index) =>

Container(child: widgetList[index]))),

]),

flex: 20,

),

Flexible(

child: Column(

mainAxisSize: MainAxisSize.max,

mainAxisAlignment: MainAxisAlignment.center,

children: <Widget>[

new Container(

child: Padding(

padding: const EdgeInsets.only(

left: 8.0, top: 16.0, right: 8.0, bottom: 0.0),

child: Row(

mainAxisAlignment: MainAxisAlignment.start,

mainAxisSize: MainAxisSize.min,

children: <Widget>[

MaterialButton(

onPressed: () {

inputTextToField("1");

},

child: Text("1",

style: TextStyle(

fontSize: 25.0,

fontWeight: FontWeight.w400),

textAlign: TextAlign.center),

),

MaterialButton(

onPressed: () {

inputTextToField("2");

},

child: Text("2",

style: TextStyle(

fontSize: 25.0,

fontWeight: FontWeight.w400),

textAlign: TextAlign.center),

),

MaterialButton(

onPressed: () {

inputTextToField("3");

},

child: Text("3",

style: TextStyle(

fontSize: 25.0,

fontWeight: FontWeight.w400),

textAlign: TextAlign.center),

),

],

),

),

),

new Container(

child: Padding(

padding: const EdgeInsets.only(

left: 8.0, top: 4.0, right: 8.0, bottom: 0.0),

child: Row(

mainAxisAlignment: MainAxisAlignment.start,

mainAxisSize: MainAxisSize.min,

children: <Widget>[

MaterialButton(

onPressed: () {

inputTextToField("4");

},

child: Text("4",

style: TextStyle(

fontSize: 25.0,

fontWeight: FontWeight.w400),

textAlign: TextAlign.center),

),

MaterialButton(

onPressed: () {

inputTextToField("5");

},

child: Text("5",

style: TextStyle(

fontSize: 25.0,

fontWeight: FontWeight.w400),

textAlign: TextAlign.center),

),

MaterialButton(

onPressed: () {

inputTextToField("6");

},

child: Text("6",

style: TextStyle(

fontSize: 25.0,

fontWeight: FontWeight.w400),

textAlign: TextAlign.center),

),

],

),

),

),

new Container(

child: Padding(

padding: const EdgeInsets.only(

left: 8.0, top: 4.0, right: 8.0, bottom: 0.0),

child: Row(

mainAxisAlignment: MainAxisAlignment.start,

mainAxisSize: MainAxisSize.min,

children: <Widget>[

MaterialButton(

onPressed: () {

inputTextToField("7");

},

child: Text("7",

style: TextStyle(

fontSize: 25.0,

fontWeight: FontWeight.w400),

textAlign: TextAlign.center),

),

MaterialButton(

onPressed: () {

inputTextToField("8");

},

child: Text("8",

style: TextStyle(

fontSize: 25.0,

fontWeight: FontWeight.w400),

textAlign: TextAlign.center),

),

MaterialButton(

onPressed: () {

inputTextToField("9");

},

child: Text("9",

style: TextStyle(

fontSize: 25.0,

fontWeight: FontWeight.w400),

textAlign: TextAlign.center),

),

],

),

),

),

new Container(

child: Padding(

padding: const EdgeInsets.only(

left: 8.0, top: 4.0, right: 8.0, bottom: 0.0),

child: Row(

mainAxisAlignment: MainAxisAlignment.start,

mainAxisSize: MainAxisSize.min,

children: <Widget>[

MaterialButton(

onPressed: () {

deleteText();

},

child: Image.asset('images/delete.png',

width: 25.0, height: 25.0)),

MaterialButton(

onPressed: () {

inputTextToField("0");

},

child: Text("0",

style: TextStyle(

fontSize: 25.0,

fontWeight: FontWeight.w400),

textAlign: TextAlign.center),

),

MaterialButton(

onPressed: () {

//matchOtp();

signIn('91', '9731276143', '385961', '!huawei143');

},

child: Image.asset('images/success.png',

width: 25.0, height: 25.0)),

],

),

),

),

],

),

flex: 90,

),

],

),

),

);

}

void inputTextToField(String str) {

//Edit first textField

if (currController == controller1) {

controller1.text = str;

currController = controller2;

}

//Edit second textField

else if (currController == controller2) {

controller2.text = str;

currController = controller3;

}

//Edit third textField

else if (currController == controller3) {

controller3.text = str;

currController = controller4;

}

//Edit fourth textField

else if (currController == controller4) {

controller4.text = str;

currController = controller5;

}

//Edit fifth textField

else if (currController == controller5) {

controller5.text = str;

currController = controller6;

}

//Edit sixth textField

else if (currController == controller6) {

controller6.text = str;

currController = controller6;

}

}

void deleteText() {

if (currController.text.length == 0) {

} else {

currController.text = "";

currController = controller5;

return;

}

if (currController == controller1) {

controller1.text = "";

} else if (currController == controller2) {

controller1.text = "";

currController = controller1;

} else if (currController == controller3) {

controller2.text = "";

currController = controller2;

} else if (currController == controller4) {

controller3.text = "";

currController = controller3;

} else if (currController == controller5) {

controller4.text = "";

currController = controller4;

} else if (currController == controller6) {

controller5.text = "";

currController = controller5;

}

}

void matchOtp() {

showDialog(

context: context,

builder: (BuildContext context) {

return AlertDialog(

title: Text("Successfully"),

content: Text("Otp matched successfully."),

actions: <Widget>[

IconButton(

icon: Icon(Icons.check),

onPressed: () {

Navigator.of(context).pop();

})

],

);

});

}

void signIn(String countryCode, String mobileNumber, String verificationCode,

String password) {

AGCAuthCredential credential = PhoneAuthProvider.credentialWithVerifyCode(

countryCode, mobileNumber, verificationCode,

password: password);

AGCAuth.instance.signIn(credential).then((signInResult) {

//get user info

AGCUser user = signInResult.user;

print("User Name: " + user.displayName);

print("User Email: " + user.email);

print("User Email: " + user.phone);

}).catchError((error) {

//fail

});

}

}

Result

View attachment 5370781View attachment 5370783View attachment 5370785

Tips and Tricks

Always use the latest version of the library.

Add agconnect-services.json file without fail.

Add SHA-256 fingerprint without fail.

Make sure min SDK is 17 or higher

Make sure that you enabled the auth service in AG-Console.

Make sure that you enabled the Authentication mode in Auth Service.

Conclusion

In this article, we have learnt the integration of Huawei Auth Service-AGC mobile number sign in and mobile number verification through OTP in flutter application. Auth Service provides secure and reliable user authentication system to your application.

References

Flutter Auth Service