-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: setup user auth commands (signin/out/whoami)
- Loading branch information
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { CAC } from "cac"; | ||
import { logger } from "../lib"; | ||
import * as dicorc from "../core/dicorc"; | ||
import exit from "exit"; | ||
|
||
export const signin = async (_: CAC, token: string): Promise<void> => { | ||
if (token.length !== 64) { | ||
logger.error("Invalid token format, make sure you copied it correctly!"); | ||
exit(1); | ||
} | ||
|
||
try { | ||
const user = await dicorc.signin(token); | ||
console.log(""); | ||
logger.success("Logged in as `%s <%s>`\n", user?.fullname, user?.email); | ||
} catch (error) { | ||
if (error.status === 401) { | ||
logger.error("Invalid token, make sure you copied it correctly!"); | ||
exit(1); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { CAC } from "cac"; | ||
import { logger } from "../lib"; | ||
import * as dicorc from "../core/dicorc"; | ||
|
||
export const signout = (_: CAC, __: { [key: string]: never }): void => { | ||
dicorc.signout(); | ||
console.log(""); | ||
logger.success("Logged out\n"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { CAC } from "cac"; | ||
import * as dicorc from "../core/dicorc"; | ||
import { logger } from "../lib"; | ||
|
||
export const whoami = async ( | ||
_: CAC, | ||
__: { [key: string]: never } | ||
): Promise<void> => { | ||
if (await dicorc.isSignedIn()) { | ||
const { user } = dicorc.read(); | ||
console.log(""); | ||
logger.info("Logged in as `%s <%s>`\n", user?.fullname, user?.email); | ||
} else { | ||
console.log(""); | ||
logger.info("Not logged in! Log in with command `login <token>`\n"); | ||
} | ||
}; |