@@ -3,34 +3,31 @@ import {Box, Newline, Text, useApp} from 'ink';
33import SelectInput from 'ink-select-input' ;
44import { Flags } from '../../lib/cli-flags.js' ;
55import { Result } from 'meow' ;
6- import TextInput , { UncontrolledTextInput } from 'ink-text-input' ;
6+ import TextInput from 'ink-text-input' ;
77import Spinner from 'ink-spinner' ;
88import os from 'os' ;
99import path from 'path' ;
10- import fs from 'fs' ;
10+ import { getToken , isTokenInEnv , saveToken } from '../../lib/token.js' ;
11+ import { Item } from '../../lib/types.js' ;
1112
12- type Item < V > = {
13- key ?: string ;
14- label : string ;
15- value : V ;
16- } ;
13+ type LoginMethod = 'token' | 'flow' ;
1714
1815export default function Login ( { cli} : { cli : Result < Flags > } ) {
19- const [ loginMethod , setLoginMethod ] = useState < string | undefined > ( undefined ) ;
20- const [ isShowingSelect , setIsShowingSelect ] = useState ( false ) ;
21- const [ isShowingInput , setIsShowingInput ] = useState ( false ) ;
16+ const [ loginMethod , setLoginMethod ] = useState < LoginMethod | undefined > (
17+ undefined ,
18+ ) ;
2219
20+ const [ isMethodSelected , setIsMethodSelected ] = useState ( false ) ;
2321 const [ isCheckingAuth , setIsCheckingAuth ] = useState ( true ) ;
2422 const [ isAuthenticated , setIsAuthenticated ] = useState ( false ) ;
2523 const [ token , setToken ] = useState < string > ( '' ) ;
2624
27- const [ isTokenInEnv , setIsTokenInEnv ] = useState ( false ) ;
2825 const [ isTokenSaved , setIsTokenSaved ] = useState ( false ) ;
2926
3027 const [ tokenInput , setTokenInput ] = useState < string > ( '' ) ;
3128 const [ saveTokenInput , setSaveTokenInput ] = useState < string > ( '' ) ;
3229
33- const loginMethodItems : Item < string > [ ] = [
30+ const loginMethodItems : Item < LoginMethod > [ ] = [
3431 {
3532 label : 'Login using access token' ,
3633 value : 'token' ,
@@ -57,14 +54,6 @@ export default function Login({cli}: {cli: Result<Flags>}) {
5754 }
5855 } , [ cli ] ) ;
5956
60- useEffect ( ( ) => {
61- setIsShowingSelect ( loginMethod === undefined ) ;
62- } , [ loginMethod ] ) ;
63-
64- useEffect ( ( ) => {
65- setIsShowingInput ( ! isShowingSelect ) ;
66- } , [ isShowingSelect ] ) ;
67-
6857 // Check token validity
6958 useEffect ( ( ) => {
7059 async function checkToken ( token : string ) {
@@ -92,56 +81,30 @@ export default function Login({cli}: {cli: Result<Flags>}) {
9281 }
9382 } , [ token , loginMethod ] ) ;
9483
95- function saveToken ( token : string ) {
96- // Save the token to .pulse-editor/config.json in user home directory
97- const configDir = path . join ( os . homedir ( ) , '.pulse-editor' ) ;
98- const configFile = path . join ( configDir , 'config.json' ) ;
99- const config = {
100- accessToken : token ,
101- } ;
102- if ( ! fs . existsSync ( configDir ) ) {
103- fs . mkdirSync ( configDir , { recursive : true } ) ;
104- }
105- fs . writeFileSync ( configFile , JSON . stringify ( config , null , 2 ) ) ;
106- }
107-
108- function getToken ( ) {
109- // First try to get the token from the environment variable
110- const tokenEnv = process . env [ 'PE_ACCESS_TOKEN' ] ;
111- if ( tokenEnv ) {
112- setIsTokenInEnv ( true ) ;
113- return tokenEnv ;
114- }
115-
116- // If not found, try to get the token from the config file
117- const configDir = path . join ( os . homedir ( ) , '.pulse-editor' ) ;
118- const configFile = path . join ( configDir , 'config.json' ) ;
119- if ( fs . existsSync ( configFile ) ) {
120- const config = JSON . parse ( fs . readFileSync ( configFile , 'utf8' ) ) ;
121- if ( config . accessToken ) {
122- return config . accessToken as string ;
123- }
124- }
125-
126- // If not found, return undefined
127- return undefined ;
128- }
84+ useEffect ( ( ) => {
85+ setTimeout ( ( ) => {
86+ setIsMethodSelected ( loginMethod !== undefined ) ;
87+ } , 0 ) ;
88+ } , [ loginMethod ] ) ;
12989
13090 return (
13191 < >
132- { isShowingSelect && (
92+ { ! cli . flags . token && ! cli . flags . flow && (
13393 < >
13494 < Text > Login to the Pulse Editor Platform</ Text >
13595 < SelectInput
13696 items = { loginMethodItems }
13797 onSelect = { item => {
13898 setLoginMethod ( item . value ) ;
13999 } }
100+ isFocused = { loginMethod === undefined }
140101 />
102+
103+ < Text > </ Text >
141104 </ >
142105 ) }
143106
144- { isShowingInput &&
107+ { isMethodSelected &&
145108 loginMethod === 'token' &&
146109 ( token . length === 0 ? (
147110 < >
@@ -166,7 +129,7 @@ export default function Login({cli}: {cli: Result<Flags>}) {
166129 ) : isAuthenticated ? (
167130 < >
168131 < Text > ✅ You are signed in successfully.</ Text >
169- { ! isTokenInEnv && getToken ( ) !== token && (
132+ { ! isTokenInEnv ( ) && getToken ( ) !== token && (
170133 < >
171134 < Text >
172135 🟢 It is recommended to save your access token as an
@@ -189,7 +152,7 @@ export default function Login({cli}: {cli: Result<Flags>}) {
189152 setIsTokenSaved ( true ) ;
190153 setTimeout ( ( ) => {
191154 exit ( ) ;
192- } , 100 ) ;
155+ } , 0 ) ;
193156 } else {
194157 exit ( ) ;
195158 }
@@ -207,7 +170,7 @@ export default function Login({cli}: {cli: Result<Flags>}) {
207170 ) : (
208171 < Text > Authentication error: please enter valid credentials.</ Text >
209172 ) ) }
210- { isShowingInput && loginMethod === 'flow' && (
173+ { isMethodSelected && loginMethod === 'flow' && (
211174 < >
212175 < Text > (WIP) Open the following URL in your browser:</ Text >
213176 < Text > https://pulse-editor.com/login</ Text >
0 commit comments