- A url shortner built with react native (frontend) and Firebase Cloud Functions (backend).
- Clone the repo to your device using
git clone https://github.com/utkarshbhatt12/url_shortner_react_native
cd app && npm install && cd .. && cd backend/functions && npm install- Follow the Official Firebase Admin SDK setup guide.
- Place your service account key (rename to
service_acc.json) in the functions/src folder. - Run
firebase deploy --only functions,hosting
- Connect your Android device to your PC and run the following command.
cd app && react-native run-android
Your app should be installed.
- Go to the main project directory in
/appand run the following commands
// source: https://stackoverflow.com/a/56520717/2758318
react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/
cd android && ./gradlew assembleDebug
-
This will generate the app-debug.apk in the
app/build/outputs/apk/debug/app-debug.apkdirectory. -
Navigate to
android/app/build/outputs/apk/debug/. Run the following:
adb install app-debug.apk
- This will install the debug apk on your device.
- Method:
POST - Headers
{
"Content-Type": "application/json",
"Authorization": "Bearer ${your token}"
}
- Responses
- Success:
{
"shortId": "e144ce220",
"originalUrl": "https://tutorialzine.com/2016/01/learn-sql-in-20-minutes"
}
```
2. Rejection:
{
"success": false,
"message": "details about the rejection..."
}
```
- Method:
GET - Headers
{}
- Responses
- Success
// 302 redirects to the originalUrl
- Failure
// Shows 404
- Method:
GET - Headers
{
"Content-Type": "application/json",
"Authorization": "Bearer ${your token}"
}
- Responses
- Success
{
success: true,
list: [{
"createdAt": 1573414701472,
"creator":"eAueexFm6be2r3kd3abDNMyhI9H2",
"hits": 10,
"originalUrl": "https://tutorialzine.com/2016/01/learn-sql-in-20-minutes",
"shortId": "e144ce220",
}
]
}
- Failure
{
"success": false,
"message": "details about the rejection..."
}
- There's only 1 collection with the name
urlMapswith an<autoId>document. - Each document will have the following structure.
{
"createdAt": 1573414701472,
"creator":"eAueexFm6be2r3kd3abDNMyhI9H2",
"hits": 10,
"originalUrl": "https://tutorialzine.com/2016/01/learn-sql-in-20-minutes",
"shortId": "e144ce220",
}
createdAtfield is the server unixtimestampat the time of creation of this document.creatoris theuidof the user who created this doc.hitsis the count the short url was accessed.originalUrlis the original url that was sent in the request.shortIdis the shortened code for this url. This id is guaranteed to be be unique during creation time.
- The urls are configured with the Firebase Cloud Functions.
- Any request to the connected domain hits the /redirect cloud function which then reads the shortId from the database and redirects the user to the appropriate url.
// .firebase.json
{
"cleanUrls": true,
"trailingSlash": false,
"ignore": [
"firebase.json",
"explorer firebase.json",
"**/.*",
"**/node_modules/**"
],
"public": "public",
"rewrites": [
{
"source": "/*",
"function": "redirector"
}
]
}
- The
rewritessection in this json does all the work. Basically anything in front of the/of our default domain will trigger theredirectorcloud function.