Skip to content

Instantly share code, notes, and snippets.

@kriss-u
Created April 4, 2023 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kriss-u/eb9345ecea0eece9751b49f8b2ddaeba to your computer and use it in GitHub Desktop.
Save kriss-u/eb9345ecea0eece9751b49f8b2ddaeba to your computer and use it in GitHub Desktop.
Transfer users between firebase accounts
#!/bin/bash
# GREEN="\e[42m"
function set_foreground_color() {
if [ -z "$1" ]
then
echo -e "$(tput setaf 1)Must pass color code from 0 to 9."
exit 1
else
echo $(tput setaf "$1")
fi
}
BLACK=$(set_foreground_color 0)
RED=$(set_foreground_color 1)
GREEN=$(set_foreground_color 2)
YELLOW=$(set_foreground_color 3)
BLUE=$(set_foreground_color 4)
MAGENTA=$(set_foreground_color 5)
CYAN=$(set_foreground_color 6)
WHITE=$(set_foreground_color 7)
NOT_SET=$(set_foreground_color 8)
RESET_COLOR=$(set_foreground_color 9)
# POSITIONAL_ARGS=()
# while [[ $# -gt 0 ]]; do
# case $1 in
# -a|--algorithm)
# ALGORITHM="$2"
# shift # past argument
# shift # past value
# ;;
# -k|--key)
# BASE64_SIGNER_KEY="$2"
# shift # past argument
# shift # past value
# ;;
# -s|--separator)
# BASE64_SALT_SEPARATOR="$2"
# shift
# shift
# ;;
# -r|--rounds)
# ROUNDS="$2"
# shift
# shift
# ;;
# -m|--mem-cost)
# MEM_COST="$2"
# shift
# shift
# ;;
# -*|--*)
# echo "${RED}Unknown option $1"
# exit 1
# ;;
# *)
# POSITIONAL_ARGS+=("$1") # save positional arg
# shift # past argument
# ;;
# esac
# done
# set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
# if [[ -z $ALGORITHM || -z $BASE64_SIGNER_KEY || -z $BASE64_SALT_SEPARATOR || -z $ROUNDS || -z $MEM_COST ]]; then
# echo "${RED}One or more arguments missing."
# echo "${BLACK}Usage: $0 -a ALGORITHM -k SIGNER_KEY -s SALT_SEPARATOR -r ROUNDS -m MEM_COST"
# fi
if ! command -v firebase &> /dev/null
then
echo $GREEN
echo "=== Installing firebase cli ==="
echo $BLACK
curl -sL https://firebase.tools | bash
fi
echo $GREEN
echo "=== Exporting users ==="
echo $GREEN
echo "=== Login firebase from old account"
echo $MAGENTA
firebase login
echo $CYAN
until read -p "Project id: " project_id && test "$project_id" != ""; do
continue
done
until read -p "Filename ending in .json or .csv: " export_filename && test $(echo "$export_filename" | grep '[[:alnum:]]\.\(json\|csv\)\>'); do
continue
done
if [[ "$?" -ne 0 ]]
then
echo $RED
echo "Login failed"
exit 1
fi
firebase auth:export "$export_filename" --project "$project_id"
if [[ "$?" -ne 0 ]]
then
echo $RED
echo "Exporting failed"
exit 1
fi
echo $GREEN
echo "=== Logout firebase ==="
firebase logout
echo $GREEN
echo "=== Login firebase from new account ==="
echo $MAGENTA
firebase login
echo $CYAN
until read -p "Importing project id: " importing_project_id && test "$importing_project_id" != ""; do
continue
done
echo $GREEN
echo "=== Importing users ==="
echo $CYAN
echo "=== Read hash parameters ==="
until read -r -p "Algorithm: " ALGORITHM && test "$ALGORITHM" != ""; do
continue
done
until read -r -p "Base64 signer key: " BASE64_SIGNER_KEY && test "$BASE64_SIGNER_KEY" != ""; do
continue
done
until read -r -p "Base64 salt separator: " BASE64_SALT_SEPARATOR && test "$BASE64_SALT_SEPARATOR" != ""; do
continue
done
until read -r -p "Rounds: " ROUNDS && test "$ROUNDS" != ""; do
continue
done
until read -r -p "Memory cost: " MEM_COST && test "$MEM_COST" != ""; do
continue
done
echo $MAGENTA
firebase auth:import "$export_filename" \
--hash-algo="$ALGORITHM" \
--hash-key="$BASE64_SIGNER_KEY" \
--salt-separator="$BASE64_SALT_SEPARATOR" \
--rounds="$ROUNDS" \
--mem-cost="$MEM_COST" \
--project="$importing_project_id"
if [[ "$?" -ne 0 ]]
then
echo "Error during import."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment