1- import React , { useState } from "react" ;
1+ import React , { useState , useEffect } from "react" ;
22import { Button , Callout , Intent , Classes } from "@blueprintjs/core" ;
33import classNames from "classnames" ;
44import "./main.sass" ;
@@ -23,30 +23,55 @@ function LoginForm() {
2323 username : "" ,
2424 password : "" ,
2525 } ) ;
26-
2726 const [ error , setError ] = useState < string | null > ( null ) ;
2827 const [ loggedIn , setLoggedIn ] = useState ( false ) ;
28+ const [ linkStraboResponse , setLinkStraboResponse ] = useState < any > ( null ) ;
29+ const [ jParam , setJParam ] = useState < string | null > ( null ) ;
30+ useEffect ( ( ) => {
31+ const params = new URLSearchParams ( window . location . search ) ;
32+ const j = params . get ( "j" ) ;
33+ if ( j ) {
34+ setJParam ( j ) ;
35+ }
36+ } , [ ] ) ;
2937
3038 const submitForm = async ( ) => {
3139 try {
32- const res = await fetch ( "http://Localhost :5500/v2/login" , {
40+ const login = await fetch ( "http://localhost :5500/v2/login" , {
3341 method : "POST" ,
3442 headers : { "Content-Type" : "application/json" } ,
35- body : JSON . stringify ( state ) ,
43+ body : JSON . stringify ( {
44+ email : state . username ,
45+ password : state . password ,
46+ } ) ,
3647 } ) ;
37-
38- if ( ! res . ok ) {
39- if ( res . status === 502 ) {
40- setError ( "The server is not available" ) ;
41- } else {
42- setError ( "Invalid credentials" ) ;
48+ const loginBody = await login . json ( ) ;
49+ console . log ( loginBody ) ;
50+ if ( login . status === 502 ) {
51+ setError ( "The server is not available" ) ;
52+ return ;
53+ }
54+ if ( login . ok ) {
55+ if ( jParam ) {
56+ const mergedBody = {
57+ ...loginBody ,
58+ strabo_jwt : jParam ,
59+ } ;
60+ const linkStrabo = await fetch (
61+ "http://localhost:5500/v2/link-strabospot" ,
62+ {
63+ method : "POST" ,
64+ headers : { "Content-Type" : "application/json" } ,
65+ body : JSON . stringify ( mergedBody ) ,
66+ }
67+ ) ;
68+ const linkStraboBody = await linkStrabo . json ( ) ;
69+ setLinkStraboResponse ( linkStraboBody ) ;
4370 }
71+ setLoggedIn ( true ) ;
72+ setError ( null ) ;
4473 return ;
4574 }
46-
47- // success
48- setLoggedIn ( true ) ;
49- setError ( null ) ;
5075 } catch ( err ) {
5176 setError ( "Something went wrong" ) ;
5277 }
@@ -61,7 +86,18 @@ function LoginForm() {
6186
6287 if ( loggedIn ) {
6388 return h ( "div" , { className : "login-page" } , [
64- h ( "h3" , "You're already logged in." ) ,
89+ h ( Callout , {
90+ title : "Login Successful" ,
91+ intent : Intent . SUCCESS ,
92+ className : "login-info" ,
93+ children : linkStraboResponse
94+ ? h (
95+ "pre" ,
96+ { className : "login-json" } ,
97+ JSON . stringify ( linkStraboResponse , null , 2 )
98+ )
99+ : { } ,
100+ } ) ,
65101 h (
66102 Button ,
67103 { intent : Intent . DANGER , onClick : ( ) => setLoggedIn ( false ) } ,
@@ -79,37 +115,43 @@ function LoginForm() {
79115 intent : Intent . DANGER ,
80116 children : error ,
81117 } ) ,
82- h ( "form.login-form" , [
83- h ( "input" , {
84- type : "text" ,
85- name : "username" ,
86- value : state . username ,
87- onChange,
88- className,
89- placeholder : "Username" ,
90- } ) ,
91- h ( "input" , {
92- type : "password" ,
93- name : "password" ,
94- value : state . password ,
95- onChange,
96- className,
97- placeholder : "Password" ,
98- onKeyUp ( e ) {
99- if ( e . key === "Enter" ) submitForm ( ) ;
118+ h (
119+ "form.login-form" ,
120+ {
121+ onSubmit : ( e ) => {
122+ e . preventDefault ( ) ; // Prevent form reload
123+ submitForm ( ) ;
100124 } ,
101- } ) ,
102- h (
103- Button ,
104- {
105- intent : Intent . PRIMARY ,
106- large : true ,
107- onClick : submitForm ,
108- disabled : ! isValid ( state ) ,
109- } ,
110- "Login"
111- ) ,
112- ] ) ,
125+ } ,
126+ [
127+ h ( "input" , {
128+ type : "text" ,
129+ name : "username" ,
130+ value : state . username ,
131+ onChange,
132+ className,
133+ placeholder : "Username" ,
134+ } ) ,
135+ h ( "input" , {
136+ type : "password" ,
137+ name : "password" ,
138+ value : state . password ,
139+ onChange,
140+ className,
141+ placeholder : "Password" ,
142+ } ) ,
143+ h (
144+ Button ,
145+ {
146+ intent : Intent . PRIMARY ,
147+ large : true ,
148+ type : "submit" , // Important: treat this as a submit button
149+ disabled : ! isValid ( state ) ,
150+ } ,
151+ "Login"
152+ ) ,
153+ ]
154+ ) ,
113155 ] ) ;
114156}
115157function InnerPage ( ) {
0 commit comments