@@ -8,7 +8,9 @@ const AUTH_DEV_BYPASS = process.env.CODRA_AUTH_DEV_BYPASS === '1';
88const ALLOW_DEV_BYPASS = process . env . CODRA_ALLOW_DEV_BYPASS === '1' || process . env . NODE_ENV !== 'production' ;
99let authToken = null ;
1010export function getAuthBaseUrl ( ) {
11- return process . env . CODRA_AUTH_BASE_URL || DEFAULT_AUTH_URL ;
11+ return process . env . CODRA_AUTH_BASE_URL ||
12+ process . env . TERA_AUTH_BASE_URL ||
13+ DEFAULT_AUTH_URL ;
1214}
1315export function getAuthFilePath ( ) {
1416 return AUTH_FILE ;
@@ -37,9 +39,8 @@ export function getAuthToken() {
3739 try {
3840 const content = fs . readFileSync ( AUTH_FILE , 'utf-8' ) ;
3941 const token = JSON . parse ( content ) ;
40- // Check if token is expired
4142 if ( new Date ( token . expiresAt ) < new Date ( ) ) {
42- console . log ( chalk . yellow ( ' Warning: Auth token has expired. Please run: codra-code login' ) ) ;
43+ console . log ( chalk . yellow ( ' Warning: Auth token has expired. Please run: codra login' ) ) ;
4344 return null ;
4445 }
4546 authToken = token ;
@@ -57,13 +58,12 @@ export async function saveAuthToken(token) {
5758 fs . mkdirSync ( codraDir , { recursive : true } ) ;
5859 }
5960 fs . writeFileSync ( AUTH_FILE , JSON . stringify ( token , null , 2 ) ) ;
60- // Set permissions to 600 on Linux/macOS
6161 if ( process . platform !== 'win32' ) {
6262 try {
6363 fs . chmodSync ( AUTH_FILE , 0o600 ) ;
6464 }
6565 catch {
66- // Ignore permission errors on some systems
66+ // Ignore
6767 }
6868 }
6969 authToken = token ;
@@ -79,12 +79,11 @@ export async function startLogin(options = {}) {
7979 console . log ( chalk . cyan ( '\n Codra Code Authentication' ) ) ;
8080 console . log ( chalk . gray ( ' Starting Tera login flow...\n' ) ) ;
8181 try {
82- // Start device auth session
8382 const startResponse = await fetch ( `${ authBaseUrl } /api/codra/auth/device/start` , {
8483 method : 'POST' ,
8584 headers : { 'Content-Type' : 'application/json' } ,
8685 body : JSON . stringify ( {
87- cli_version : '0.1.6 ' ,
86+ cli_version : '0.4.0 ' ,
8887 platform : process . platform
8988 } )
9089 } ) ;
@@ -105,18 +104,16 @@ export async function startLogin(options = {}) {
105104 console . log ( chalk . gray ( ' Opening browser for authentication...' ) ) ;
106105 console . log ( chalk . gray ( ' If browser doesn\'t open, visit:\n' ) ) ;
107106 console . log ( chalk . cyan ( ` ${ verification_url } \n` ) ) ;
108- // Try to open browser
109107 await openBrowser ( verification_url ) ;
110108 }
111109 console . log ( chalk . gray ( ' Waiting for authentication...' ) ) ;
112110 console . log ( chalk . gray ( ' (Press Ctrl+C to cancel)\n' ) ) ;
113- // Poll for authentication
114111 const token = await pollForAuth ( device_code , authBaseUrl , interval || 2 ) ;
115112 if ( token ) {
116113 await saveAuthToken ( token ) ;
117114 console . log ( chalk . green ( '\n ✓ Authenticated with Tera successfully!' ) ) ;
118115 console . log ( chalk . gray ( ` Account: ${ token . email } ` ) ) ;
119- console . log ( chalk . gray ( ' You can now use Codra Code.\n' ) ) ;
116+ console . log ( chalk . gray ( ' You can now use Codra Code for hosted features .\n' ) ) ;
120117 return true ;
121118 }
122119 console . log ( chalk . red ( '\n ✗ Authentication failed or timed out.\n' ) ) ;
@@ -127,14 +124,6 @@ export async function startLogin(options = {}) {
127124 return false ;
128125 }
129126}
130- function generateDeviceCode ( ) {
131- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
132- let result = '' ;
133- for ( let i = 0 ; i < 32 ; i ++ ) {
134- result += chars . charAt ( Math . floor ( Math . random ( ) * chars . length ) ) ;
135- }
136- return result ;
137- }
138127async function openBrowser ( url ) {
139128 const { exec } = await import ( 'child_process' ) ;
140129 const platform = process . platform ;
@@ -148,12 +137,10 @@ async function openBrowser(url) {
148137 else {
149138 command = `xdg-open "${ url } " || echo "Could not open browser"` ;
150139 }
151- return new Promise ( ( resolve ) => {
152- exec ( command , ( ) => resolve ( ) ) ;
153- } ) ;
140+ return new Promise ( ( resolve ) => { exec ( command , ( ) => resolve ( ) ) ; } ) ;
154141}
155142async function pollForAuth ( deviceCode , authBaseUrl , interval ) {
156- const maxAttempts = 150 ; // 5 minutes with 2-second intervals
143+ const maxAttempts = 150 ;
157144 const pollInterval = interval * 1000 ;
158145 for ( let i = 0 ; i < maxAttempts ; i ++ ) {
159146 try {
@@ -185,7 +172,7 @@ async function pollForAuth(deviceCode, authBaseUrl, interval) {
185172 }
186173 }
187174 catch {
188- // Backend endpoint may not exist yet
175+ // safe placeholder, backend may not be live yet
189176 }
190177 await new Promise ( resolve => setTimeout ( resolve , pollInterval ) ) ;
191178 }
@@ -195,7 +182,7 @@ export async function authStatus() {
195182 console . log ( chalk . cyan ( '\n Auth Status' ) ) ;
196183 if ( AUTH_DEV_BYPASS && ALLOW_DEV_BYPASS ) {
197184 console . log ( chalk . yellow ( ' Mode: Development bypass active' ) ) ;
198- console . log ( chalk . gray ( ' Set CODRA_AUTH_DEV_BYPASS=0 to disable\n' ) ) ;
185+ console . log ( chalk . gray ( ' Set CODRA_AUTH_DEV_BYPASS=*** to disable\n' ) ) ;
199186 return ;
200187 }
201188 const token = getAuthToken ( ) ;
@@ -206,22 +193,25 @@ export async function authStatus() {
206193 console . log ( chalk . gray ( ` Account: ${ token . email } ` ) ) ;
207194 console . log ( chalk . gray ( ` Expires: ${ expiresDate . toLocaleDateString ( ) } ` ) ) ;
208195 if ( isExpired ) {
209- console . log ( chalk . yellow ( ' Warning: Token has expired. Run: codra-code login' ) ) ;
196+ console . log ( chalk . yellow ( ' Warning: Token has expired. Run: codra login' ) ) ;
210197 }
211- console . log ( chalk . gray ( ` Token: ${ AUTH_FILE } ` ) ) ;
198+ console . log ( chalk . gray ( ` Hosted usage: available (Tera/Talocode account)` ) ) ;
199+ console . log ( chalk . gray ( ` Token file: ${ AUTH_FILE } (contents never printed)` ) ) ;
212200 }
213201 else {
214202 console . log ( chalk . red ( ' Status: Not authenticated' ) ) ;
215- console . log ( chalk . gray ( ' Run: codra-code login' ) ) ;
203+ console . log ( chalk . gray ( ' Run: codra login' ) ) ;
216204 console . log ( chalk . gray ( ' Sign in at: https://teraai.chat/auth/signin' ) ) ;
205+ console . log ( chalk . gray ( ' Hosted usage: blocked for hosted providers' ) ) ;
206+ console . log ( chalk . gray ( ' Local mode (mock, ollama): works without auth' ) ) ;
217207 }
218208 console . log ( '' ) ;
219209}
220210export function requireAuth ( ) {
221211 if ( isAuthenticated ( ) )
222212 return true ;
223- console . log ( chalk . red ( '\n Codra Code requires a Tera account.' ) ) ;
224- console . log ( chalk . gray ( ' Run: codra-code login' ) ) ;
213+ console . log ( chalk . red ( '\n Codra Code requires a Tera account for hosted features .' ) ) ;
214+ console . log ( chalk . gray ( ' Run: codra login' ) ) ;
225215 console . log ( chalk . gray ( ' Sign in at: https://teraai.chat/auth/signin\n' ) ) ;
226216 return false ;
227217}
0 commit comments