11
22import Head from 'next/head'
33import { useState , useEffect } from 'react' ;
4-
5- let url = "http://localhost:8000" //"https://django-todo-list-api.herokuapp.com"
4+ import { useRouter } from 'next/router'
5+ const { url} = require ( '../config' )
66const HeadTag = function ( ) {
77
88
@@ -54,9 +54,25 @@ const HeadTag = function () {
5454
5555const Login = function ( ) {
5656
57+ const router = useRouter ( ) ;
58+
5759 const [ formData , setForm ] = useState ( { username : 'Damon' , password : '123456' } )
5860 useEffect ( async ( ) => {
59- } )
61+
62+ let req = await fetch ( `${ url } /api/todo/` , { credentials :'include' } ) ;
63+
64+ if ( req . status == 200 ) {
65+
66+
67+ router . replace ( '/todo' )
68+ }
69+
70+
71+
72+
73+
74+
75+ } , [ ] )
6076
6177
6278 const submitForm = async function ( ) {
@@ -65,7 +81,7 @@ const Login = function () {
6581
6682 // return console.log(form);
6783
68- const req = await fetch ( 'https://django-todo-list- api.herokuapp.com/api/ login/' , {
84+ const req = await fetch ( ` ${ url } / api/ login/` , {
6985
7086 method : 'POST' ,
7187 headers : {
@@ -83,17 +99,15 @@ const Login = function () {
8399
84100 if ( req . status === 200 ) {
85101
86- let res = await req . json ( )
87-
88- console . log ( req . status , res ) ;
89-
102+ let res = await req . json ( )
90103
104+ console . log ( res ) ;
91105 }
92106
93107
94108 else {
95109
96- alert ( "Something went wrong" )
110+ alert ( "Something went wrong from server " )
97111 }
98112
99113
@@ -118,7 +132,6 @@ const Login = function () {
118132
119133 < HeadTag />
120134
121- < main >
122135
123136
124137 < div className = "container" >
@@ -174,7 +187,6 @@ const Login = function () {
174187
175188 </ div >
176189
177- </ main >
178190 </ div >
179191
180192 )
0 commit comments