Flutter
...
Authentication
User Login
15 min
user login and logout for flutter using parse server introduction after implementing the user registration for flutter on parse in the last guide, you will learn how to login and logout users using the same parseuser class after a signup, the login operation is performed automatically, and a new user session is created the logout operation deletes the active session object for the logged user in this guide, you will learn how to use the flutter plugin for parse server to perform login/logout using parseuser class for your flutter app goal to build a user login/logout feature using parse for a flutter app prerequisites to complete this tutorial, you will need flutter version 2 2 x or later https //flutter dev/docs/get started/install android studio https //developer android com/studio or vs code installed (with plugins dart and flutter) an app created on back4app note follow the new parse app tutorial to learn how to create a parse app on back4app an flutter app connected to back4app note follow the install parse sdk on flutter project to create an flutter project connected to back4app complete the previous guide so you can have a better understanding of the parseuser parseuser class a device (or virtual device) running android or ios understanding the login/logout app to better understand the login/singout process, we will create an app to login e logout user on your account we won’t explain the flutter application code once this guide’s primary focus is using the flutter with parse following the next steps, you will build a login e logout app at back4app database let’s get started! in the following steps, you will be able to build a login/logout app 1 create the login/logout app template open your flutter project from the previous guide flutter plugin for parse server go to the main dart main dart file, clean up all the code, and replace it with 1 import 'package\ flutter/material dart'; 2 import 'package\ parse server sdk flutter/parse server sdk dart'; 3 4 void main() async { 5 widgetsflutterbinding ensureinitialized(); 6 7 final keyapplicationid = 'your app id here'; 8 final keyclientkey = 'your client key here'; 9 final keyparseserverurl = 'https //parseapi back4app com'; 10 11 await parse() initialize(keyapplicationid, keyparseserverurl, 12 clientkey keyclientkey, debug true); 13 14 runapp(myapp()); 15 } 16 17 class myapp extends statelesswidget { 18 @override 19 widget build(buildcontext context) { 20 return materialapp( 21 title 'flutter login/logout', 22 theme themedata( 23 primaryswatch colors blue, 24 visualdensity visualdensity adaptiveplatformdensity, 25 ), 26 home homepage(), 27 ); 28 } 29 } 30 31 class homepage extends statefulwidget { 32 @override 33 homepagestate createstate() => homepagestate(); 34 } 35 36 class homepagestate extends state\<homepage> { 37 final controllerusername = texteditingcontroller(); 38 final controllerpassword = texteditingcontroller(); 39 bool isloggedin = false; 40 41 @override 42 widget build(buildcontext context) { 43 return scaffold( 44 appbar appbar( 45 title const text('flutter login/logout'), 46 ), 47 body center( 48 child singlechildscrollview( 49 padding const edgeinsets all(8), 50 child column( 51 crossaxisalignment crossaxisalignment stretch, 52 children \[ 53 container( 54 height 200, 55 child image network( 56 'http //blog back4app com/wp content/uploads/2017/11/logo b4a 1 768x175 1 png'), 57 ), 58 center( 59 child const text('flutter on back4app', 60 style 61 textstyle(fontsize 18, fontweight fontweight bold)), 62 ), 63 sizedbox( 64 height 16, 65 ), 66 center( 67 child const text('user login/logout', 68 style textstyle(fontsize 16)), 69 ), 70 sizedbox( 71 height 16, 72 ), 73 textfield( 74 controller controllerusername, 75 enabled !isloggedin, 76 keyboardtype textinputtype text, 77 textcapitalization textcapitalization none, 78 autocorrect false, 79 decoration inputdecoration( 80 border outlineinputborder( 81 borderside borderside(color colors black)), 82 labeltext 'username'), 83 ), 84 sizedbox( 85 height 8, 86 ), 87 textfield( 88 controller controllerpassword, 89 enabled !isloggedin, 90 obscuretext true, 91 keyboardtype textinputtype text, 92 textcapitalization textcapitalization none, 93 autocorrect false, 94 decoration inputdecoration( 95 border outlineinputborder( 96 borderside borderside(color colors black)), 97 labeltext 'password'), 98 ), 99 sizedbox( 100 height 16, 101 ), 102 container( 103 height 50, 104 child textbutton( 105 child const text('login'), 106 onpressed isloggedin ? null () => douserlogin(), 107 ), 108 ), 109 sizedbox( 110 height 16, 111 ), 112 container( 113 height 50, 114 child textbutton( 115 child const text('logout'), 116 onpressed !isloggedin ? null () => douserlogout(), 117 ), 118 ) 119 ], 120 ), 121 ), 122 )); 123 } 124 125 void showsuccess(string message) { 126 showdialog( 127 context context, 128 builder (buildcontext context) { 129 return alertdialog( 130 title const text("success!"), 131 content text(message), 132 actions \<widget>\[ 133 new textbutton( 134 child const text("ok"), 135 onpressed () { 136 navigator of(context) pop(); 137 }, 138 ), 139 ], 140 ); 141 }, 142 ); 143 } 144 145 void showerror(string errormessage) { 146 showdialog( 147 context context, 148 builder (buildcontext context) { 149 return alertdialog( 150 title const text("error!"), 151 content text(errormessage), 152 actions \<widget>\[ 153 new textbutton( 154 child const text("ok"), 155 onpressed () { 156 navigator of(context) pop(); 157 }, 158 ), 159 ], 160 ); 161 }, 162 ); 163 } 164 165 void douserlogin() async { 166 167 } 168 169 void douserlogout() async { 170 171 } 172 } 173 when debug debug parameter in function parse() initialize parse() initialize is true true , allows displaying parse api calls on the console this configuration can assist in debugging the code it is advisable to disable debug in the release version 2 connect template to back4app project find your application id and client key credentials navigating to your app dashboard at back4app website https //www back4app com/ update your code in main dart main dart with the values of your project’s applicationid and clientkey in back4app keyapplicationid = app id keyclientkey = client key run the project, and the app will load as shown in the image 3 code for login user the user login function creates a session session object, which points to the user user logged in and stores in your local storage a valid user session future calls to methods like currentuser currentuser will successfully retrieve your user user data and sessiontoken sessiontoken for session session object which created in the dashboard dashboard search for the function douserlogin douserlogin in the file main dart main dart replace the code inside douserlogin douserlogin with 1 final username = controllerusername text trim(); 2 final password = controllerpassword text trim(); 3 4 final user = parseuser(username, password, null); 5 6 var response = await user login(); 7 8 if (response success) { 9 showsuccess("user was successfully login!"); 10 setstate(() { 11 isloggedin = true; 12 }); 13 } else { 14 showerror(response error! message); 15 } to build this function, follow these steps create a new parseuser parseuser class instance with the command parseuser(username, password, null); parseuser(username, password, null); using the data entered in the app the e mail field is not necessary and must be informed with null call the login login function, which will create a session session in your database in the parse dashboard and save the token to local storage check if the user login was successful if it wasn’t successful, show the error description message the complete function should look like this 1 void douserlogin() async { 2 final username = controllerusername text trim(); 3 final password = controllerpassword text trim(); 4 5 final user = parseuser(username, password, null); 6 7 var response = await user login(); 8 9 if (response success) { 10 showsuccess("user was successfully login!"); 11 setstate(() { 12 isloggedin = true; 13 }); 14 } else { 15 showerror(response error! message); 16 } 17 } to test it, click on the run run button in android studio/vscode after providing the desired user credentials, you will see this message after pressing on login if everything was successful error handling can be tested if you try to login a user with invalid credentials you will get another error if you try to login with no password 4 code for logout user the user logout function deletes the session session object, which was created in the login function it will clear this session on the device and log out of any linked services in your parse server search for the function douserlogout douserlogout in the file main dart main dart replace the code inside douserlogout douserlogout with 1 final user = await parseuser currentuser() as parseuser; 2 var response = await user logout(); 3 4 if (response success) { 5 showsuccess("user was successfully logout!"); 6 setstate(() { 7 isloggedin = false; 8 }); 9 } else { 10 showerror(response error! message); 11 } to build this function, follow these steps get the current logged user using function parseuser currentuser() parseuser currentuser() call the logout logout function for parseuser parseuser object, which will delete session session in your database and clean the token in the local storage check if the user logout was successfull if it wasn’t successful, show the error description message the complete code should look like this 1 void douserlogout() async { 2 final user = await parseuser currentuser() as parseuser; 3 var response = await user logout(); 4 5 if (response success) { 6 showsuccess("user was successfully logout!"); 7 setstate(() { 8 isloggedin = false; 9 }); 10 } else { 11 showerror(response error! message); 12 } 13 } to test it, click on the run run button in android studio/vscode after providing the desired user credentials, you will see this message after pressing on login if everything was successful click the “logout” button it’s done! at the end of this guide, you can login and logout parse users of your app using parse server core features through back4app!