Implementing Login Functionality in SUSI Web Chat

SUSI Web Chat is fully equipped with all the accounting features which are being provided by the SUSI.AI API. This blog discloses all the API features one needs to know to embed the Login functionality in SUSI Web Chat. To embed the Login feature, first we create a form using material-ui.com components with the followng fields Email Password Note: We can also chose a Custom Server while logging in, here I have used the Standard Server ie. http://api.susi.ai to make the user Login The form can be made with the help of the following fields TextField for Email, props to be passed Name - email Value - this.state.email which gets the value of the current email floatingLabelText is Email, errorText is the message which we want to show when the email does not match the regex or its empty. Code Snippet - <TextField name="email" value={this.state.email} onChange={this.handleChange} errorText={this.emailErrorMessage} floatingLabelText="Email" /> PasswordField for Password Name - password Value - this.state.password which gets the value of the current email floatingLabelText is Password, errorText is the message which we want to show when the password is not filled. Code Snippet- <PasswordField name='password' value={this.state.password} onChange={this.handleChange} errorText={this.passwordErrorMessage} floatingLabelText='Password' /> The next elements are RadioButton groups taken from material-ui.com. This ensures the user signs into a standard server or even to a custom server. This is not compulsory as of now. And lastly we need a submit button, which is disabled until all the fields are filled. Code Snippet - <RaisedButton label="Login" type="submit" labelColor="#fff" disable={!this.state.validForm} /> For the full form, check out this file at Login.react.js A Sample UI could be as shown in the image Next after creating the Login Screen, we make the onSubmit prop which is to be hooked up with another function called handleSubmit. An example code snippet from Login.react.js- handleSubmit = (e) => { e.preventDefault(); // Get the trimmed values from the fields var email = this.state.email.trim(); var password = this.state.password.trim(); // Set the default server to login let BASE_URL = defaults.Server; // handle all the details of the chosen server let serverUrl = this.state.serverUrl; if(serverUrl.slice(-1) === '/'){ serverUrl = serverUrl.slice(0,-1); } if(serverUrl !== ''){ BASE_URL = serverUrl; } // if email and password is filled return true if (!email || !password) { return this.state.isFilled; } // Check the regex of email let validEmail = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(email); // Pass the parameters to the loginEndPoint let loginEndPoint = BASE_URL+'/aaa/login.json?type=access-token&login=' + this.state.email + '&password=' + this.state.password; // If email and password is filled and valid call AJAX if (email && validEmail) { // AJAX Calls } } Then we make the Ajax Calls and store the created token from hitting the URL at http://api.susi.ai/aaa/login.json?type=access-token&login=EMAIL&password=PASSWORD. We store the cookie in browser and generate a session for the user using a package ‘universal-cookies’. $.ajax({ url: loginEndPoint, dataType: 'jsonp', jsonpCallback: 'p', jsonp: 'callback', crossDomain: true, success: function (response) { cookies.set('serverUrl', BASE_URL, { path: '/' }); let accessToken = response.access_token; let state = this.state;// Adding the current State let time = response.valid_seconds; // Get the valid time of the…

Continue ReadingImplementing Login Functionality in SUSI Web Chat