templates/authenticated-session.sh
templates/authenticated-session.shBrowse 14 files
1,138 tokens
4,912 bytes
Token encoding: o200k_base
Snapshot aff6125
← Back to SKILL.md
1#!/bin/bash2# Template: Authenticated Session Workflow3# Purpose: Login once, save state, reuse for subsequent runs4# Usage: ./authenticated-session.sh <login-url> [state-file]5#6# RECOMMENDED: Use the auth vault instead of this template:7# echo "<pass>" | agent-browser auth save myapp --url <login-url> --username <user> --password-stdin8# agent-browser auth login myapp9# The auth vault stores credentials securely and the LLM never sees passwords.10#11# Environment variables:12# APP_USERNAME - Login username/email13# APP_PASSWORD - Login password14#15# Two modes:16# 1. Discovery mode (default): Shows form structure so you can identify refs17# 2. Login mode: Performs actual login after you update the refs18#19# Setup steps:20# 1. Run once to see form structure (discovery mode)21# 2. Update refs in LOGIN FLOW section below22# 3. Set APP_USERNAME and APP_PASSWORD23# 4. Delete the DISCOVERY section24 25set -euo pipefail26 27LOGIN_URL="${1:?Usage: $0 <login-url> [state-file]}"28STATE_FILE="${2:-./auth-state.json}"29 30echo "Authentication workflow: $LOGIN_URL"31 32# ================================================================33# SAVED STATE: Skip login if valid saved state exists34# ================================================================35if [[ -f "$STATE_FILE" ]]; then36 echo "Loading saved state from $STATE_FILE..."37 if agent-browser --state "$STATE_FILE" open "$LOGIN_URL" 2>/dev/null; then38 agent-browser wait --load load39 40 CURRENT_URL=$(agent-browser get url)41 if [[ "$CURRENT_URL" != *"login"* ]] && [[ "$CURRENT_URL" != *"signin"* ]]; then42 echo "Session restored successfully"43 agent-browser snapshot -i44 exit 045 fi46 echo "Session expired, performing fresh login..."47 agent-browser close 2>/dev/null || true48 else49 echo "Failed to load state, re-authenticating..."50 fi51 rm -f "$STATE_FILE"52fi53 54# ================================================================55# DISCOVERY MODE: Shows form structure (delete after setup)56# ================================================================57echo "Opening login page..."58agent-browser open "$LOGIN_URL"59agent-browser wait --load domcontentloaded60agent-browser wait --fn "(() => { const usable = (el) => { const style = getComputedStyle(el); return !el.disabled && !el.readOnly && style.display !== 'none' && style.visibility !== 'hidden' && el.getClientRects().length > 0; }; const username = Array.from(document.querySelectorAll('input[type=email], input[type=text], input[autocomplete=username], input[name*=email i], input[name*=user i], input[name*=login i]')).some(usable); const password = Array.from(document.querySelectorAll('input[type=password]')).some(usable); return username && password; })()"61 62echo ""63echo "Login form structure:"64echo "---"65agent-browser snapshot -i66echo "---"67echo ""68echo "Next steps:"69echo " 1. Note the refs: username=@e?, password=@e?, submit=@e?"70echo " 2. Update the LOGIN FLOW section below with your refs"71echo " 3. Set: export APP_USERNAME='...' APP_PASSWORD='...'"72echo " 4. Delete this DISCOVERY MODE section"73echo ""74agent-browser close75exit 076 77# ================================================================78# LOGIN FLOW: Uncomment and customize after discovery79# ================================================================80# : "${APP_USERNAME:?Set APP_USERNAME environment variable}"81# : "${APP_PASSWORD:?Set APP_PASSWORD environment variable}"82#83# agent-browser open "$LOGIN_URL"84# agent-browser wait --load domcontentloaded85# agent-browser wait --fn "(() => { const usable = (el) => { const style = getComputedStyle(el); return !el.disabled && !el.readOnly && style.display !== 'none' && style.visibility !== 'hidden' && el.getClientRects().length > 0; }; const username = Array.from(document.querySelectorAll('input[type=email], input[type=text], input[autocomplete=username], input[name*=email i], input[name*=user i], input[name*=login i]')).some(usable); const password = Array.from(document.querySelectorAll('input[type=password]')).some(usable); return username && password; })()"86# agent-browser snapshot -i87#88# # Fill credentials (update refs to match your form)89# agent-browser fill @e1 "$APP_USERNAME"90# agent-browser fill @e2 "$APP_PASSWORD"91# agent-browser click @e392# Replace this with the app-specific post-login wait, for example:93# agent-browser wait --url "**/dashboard"94# agent-browser wait --text "Welcome"95#96# # Verify login succeeded97# FINAL_URL=$(agent-browser get url)98# if [[ "$FINAL_URL" == *"login"* ]] || [[ "$FINAL_URL" == *"signin"* ]]; then99# echo "Login failed - still on login page"100# agent-browser screenshot /tmp/login-failed.png101# agent-browser close102# exit 1103# fi104#105# # Save state for future runs106# echo "Saving state to $STATE_FILE"107# agent-browser state save "$STATE_FILE"108# echo "Login successful"109# agent-browser snapshot -i110