It will create a client ID and secret key.
Now, in Visual Studio code, go to src folder and create a new folder and inside this folder add 2 new components
1.Logintbygoogle.js
2. Dashboard.js
Add Routing in ReactJS
Install react-router-dom package by using the following command
Open app.js file and imports of Router and Route (react-router-dom) and 2 components.
Now, open the Logintbygoogle.js file and add the following code.
Now, open the Dashboard .js file and add the following code.
- import React, { Component } from 'react'
-
- export class Dashboard extends Component {
- constructor(props){
- super(props);
- this.state = {
- name:'',
- };
- }
- componentDidMount() {
- const data = JSON.parse(sessionStorage.getItem('userData'));
- let data1=data;
- console.log(data1.data.Name);
-
- console.log(data1.Name);
- this.setState({name: data1.data.Name})
- }
- render() {
- return (
- <div className="container">
- <div className="row">
- <div className="col-sm-12 btn btn-info">
- Welcome to Dashboard
- </div>
- </div>
- <div className="row">
- <div className="col-sm-3"> Welcome :{this.state.name} </div>
- <div className="col-sm-9"></div>
- {}
- </div>
- </div>
- )
- }
- }
-
- export default Dashboard
Create a table in the Database
Open SQL Server Management Studio, create a database named "Demotest," and in this database, create a table. Give that table a name like "sociallogin".
Create a Web API Project
Open Visual Studio and create a new project.
Change the name as LoginApplication
Select Web API as its template.
Right-click the Models folder from Solution Explorer and go to Add >> New Item >> data.
Click on the "ADO.NET Entity Data Model" option and click "Add".
Select the EF Designer from the database and click the "Next" button.
Add the connection properties and select database name on the next page and click OK.
Check the Table checkbox
. The internal options will be selected by default. Now, click the "Finish" button.
Our data model is created successfully now.
Now, Right-click on the model folder and add two classes -Userdetails and Response. Now, paste the following code in these classes.
Userdetails class
- public class Userdetails
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string Email { get; set; }
- public string ProviderName { get; set; }
- public string Image { get; set; }
- public string Token { get; set; }
- }
Response Class
- public class Response
- {
- public string Status { set; get; }
- public string Message { set; get; }
- }
Right-click on the Controllers folder and add a new controller. Name it as "Login controller" and add the following namespace.
- using LoginWithSocialMedio.Models;
Create a method in this controller to save data. Add the following code in this controller.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using LoginWithSocialMedio.Models;
-
- namespace LoginWithSocialMedio.Controllers
- {
- [RoutePrefix("Api/Login")]
- public class LoginController : ApiController
- {
- [Route("SocialmediaData")]
- [HttpPost]
- public object SocialmediaData(Userdetails user)
- {
- try
- {
- DemoTestEntities DB = new DemoTestEntities();
- Socaillogin Social = new Socaillogin();
- if (Social.Id == 0)
- {
- Social.Name = user.Name;
- Social.Email = user.Email;
- Social.ProviderName = user.ProviderName;
- Social.Image = user.Image;
- Social.Token = user.Token;
- var res = DB.Socaillogins.Add(Social);
- DB.SaveChanges();
- return res;
- }
- }
- catch (Exception)
- {
- throw;
- }
- return new Response
- { Status = "Error", Message = "Data." };
- }
- }
- }
Now, let's enable Cors. Go to Tools, open NuGet Package Manager, search for Cors and install the Microsoft.Asp.Net.WebApi.Cors package. Open Webapiconfig.cs, and add the following lines.
- EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*");
- config.EnableCors(cors);
Now to go Visual Studio code and run the project
Click on 'login with google' button
Enter e-mail and password
Now if login is successful then it redirects to the dashboard page
Summary
In this article, we discussed the process of logging in with Gmail using React and Web API.