Skip to content

Commit

Permalink
adds script and instructions to refresh token without manual copy paste
Browse files Browse the repository at this point in the history
  • Loading branch information
jduss4 committed Apr 26, 2024
1 parent 049a8f2 commit 6915577
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ To obtain your token, first log into the correct environment in your CLI:
cf login -a [your cf domain] --sso
```

Then run:
You can view your token at `cf oauth-token` and manually copy everything after "bearer" into `CF_API_TOKEN`, or you can run a script to do this for you:

```
cf oauth-token
./token-refresh.sh
```

Copy this token and set it as your `CF_API_TOKEN` in `env.local`. Do not copy "bearer" at the beginning of the token.
This command will be run automatically when you start your application if you use `npm run dev-cf`.

Note that oauth tokens expire frequently. To obtain a new token, just run `cf oauth-token` again and replace your previous variable value with the new one.

Expand All @@ -81,6 +81,10 @@ docker-compose up
Then run the dev server:

```bash
# to run with a valid cloud foundry token
npm run dev-cf

# to run without accessing cloud foundry resources
npm run dev
```

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"scripts": {
"build": "next build",
"dev": "next dev",
"dev-cf": "./token-refresh.sh; next dev",
"start": "next start",
"test": "jest",
"lint": "next lint",
Expand Down
19 changes: 19 additions & 0 deletions token-refresh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#! /usr/bin/env bash

# Quick script that obtains the token from cloud foundry
# and adds it to the user's local environment variables
#
# Note: you must be logged into the cloud foundry CLI
# for this script to work

file=".env.local"

cf_res=$(cf oauth-token)
token=${cf_res#* }

if [ "$token" == "FAILED" ]; then
exit 1
else
sed -i '' -e "s/CF_API_TOKEN=.*/CF_API_TOKEN=${token}/" $file
echo "Added token to ${file}"
fi

0 comments on commit 6915577

Please sign in to comment.