Eric Whale

Connect weather APIs to user info

...@@ -49,6 +49,8 @@ function App() { ...@@ -49,6 +49,8 @@ function App() {
49 </button> 49 </button>
50 </div> 50 </div>
51 51
52 + <hr></hr>
53 +
52 {!weather ? ( 54 {!weather ? (
53 "" 55 ""
54 ) : ( 56 ) : (
...@@ -59,13 +61,13 @@ function App() { ...@@ -59,13 +61,13 @@ function App() {
59 </h2> 61 </h2>
60 <h3>* {weather.description} *</h3> 62 <h3>* {weather.description} *</h3>
61 <p> 63 <p>
62 - Temperature : {weather.temp.realCelcius} / feels like{" "} 64 + 🌡 : {weather.temp.realCelcius} / feels like{" "}
63 {weather.temp.feelCelcius} C 65 {weather.temp.feelCelcius} C
64 </p> 66 </p>
65 - <p>Wind : {weather.types.wind} m/s</p> 67 + <p>🌬 : {weather.types.wind} m/s</p>
66 - <p>Cloud : {weather.types.clouds} %</p> 68 + <p>☁️ : {weather.types.clouds} %</p>
67 - <p>{weather.rain ? "rain : Yes" : "rain : No"}</p> 69 + <p>{weather.rain ? "☔️ : Yes" : "☔️ : No"}</p>
68 - <p>{weather.snow ? "snow : Yes" : "snow : No"}</p> 70 + <p>{weather.snow ? "❄️ : Yes" : "❄️ : No"}</p>
69 </div> 71 </div>
70 )} 72 )}
71 73
...@@ -99,7 +101,7 @@ function App() { ...@@ -99,7 +101,7 @@ function App() {
99 ) : ( 101 ) : (
100 <div> 102 <div>
101 <h2> 103 <h2>
102 - {air.meta.country} {air.meta.state ? air.meta.state : ""} 104 + {air.meta.state ? air.meta.state : ""} {air.meta.country}
103 </h2> 105 </h2>
104 <p>CO : {air.airData.co} μg/m3</p> 106 <p>CO : {air.airData.co} μg/m3</p>
105 <p>NH3 : {air.airData.nh3} μg/m3</p> 107 <p>NH3 : {air.airData.nh3} μg/m3</p>
......
...@@ -5,7 +5,10 @@ const getWeather = async (user) => { ...@@ -5,7 +5,10 @@ const getWeather = async (user) => {
5 return; 5 return;
6 } 6 }
7 try { 7 try {
8 - const response = await axios.post("http://localhost:8080/api/weather"); 8 + const userToken = JSON.parse(sessionStorage.getItem("user-token"));
9 + const response = await axios.post("http://localhost:8080/api/weather", {
10 + token: userToken.token,
11 + });
9 const data = response.data; 12 const data = response.data;
10 const formattedData = { 13 const formattedData = {
11 meta: { 14 meta: {
...@@ -40,8 +43,10 @@ const getForecaset = async (user) => { ...@@ -40,8 +43,10 @@ const getForecaset = async (user) => {
40 return; 43 return;
41 } 44 }
42 try { 45 try {
46 + const userToken = JSON.parse(sessionStorage.getItem("user-token"));
43 const response = await axios.post( 47 const response = await axios.post(
44 - "http://localhost:8080/api/weather/forecast" 48 + "http://localhost:8080/api/weather/forecast",
49 + { token: userToken.token }
45 ); 50 );
46 const data = response.data; 51 const data = response.data;
47 52
...@@ -85,8 +90,10 @@ const getAirPollution = async (user) => { ...@@ -85,8 +90,10 @@ const getAirPollution = async (user) => {
85 return; 90 return;
86 } 91 }
87 try { 92 try {
93 + const userToken = JSON.parse(sessionStorage.getItem("user-token"));
88 const response = await axios.post( 94 const response = await axios.post(
89 - "http://localhost:8080/api/weather/airpollution" 95 + "http://localhost:8080/api/weather/airpollution",
96 + { token: userToken.token }
90 ); 97 );
91 const dataObject = response.data; 98 const dataObject = response.data;
92 const airData = dataObject.airPollutionData.list[0].components; 99 const airData = dataObject.airPollutionData.list[0].components;
......
1 * { 1 * {
2 margin: 0; 2 margin: 0;
3 padding: 0; 3 padding: 0;
4 + background-color: #FFDEAD;
4 } 5 }
5 6
6 .container { 7 .container {
...@@ -46,3 +47,8 @@ ...@@ -46,3 +47,8 @@
46 font-size: 0.7rem; 47 font-size: 0.7rem;
47 } 48 }
48 } 49 }
50 +
51 +hr {
52 + margin-top: 0.5rem;
53 + margin-bottom: 0.3rem;
54 +}
......
1 const bcrypt = require("bcryptjs"); 1 const bcrypt = require("bcryptjs");
2 +const jwt = require("jsonwebtoken");
2 const axios = require("axios").default; 3 const axios = require("axios").default;
3 const User = require("../models/userModel"); 4 const User = require("../models/userModel");
4 // handles "exception" inside of async express routes 5 // handles "exception" inside of async express routes
...@@ -8,10 +9,16 @@ const asyncHandler = require("express-async-handler"); ...@@ -8,10 +9,16 @@ const asyncHandler = require("express-async-handler");
8 // @route Get /api/weather 9 // @route Get /api/weather
9 // @access Public 10 // @access Public
10 const getWeather = asyncHandler(async (req, res) => { 11 const getWeather = asyncHandler(async (req, res) => {
11 - const countryCode = "US"; 12 + const token = req.body.token;
12 - const city = "los angeles"; 13 + const decoded = jwt.decode(token, { complete: true });
13 - const limit = 5; 14 + const user = await User.findById(decoded.payload.id)
15 + .select("-password")
16 + .select("-email");
17 +
18 + const countryCode = user.country;
19 + const city = user.city;
14 20
21 + const limit = 5;
15 try { 22 try {
16 const metaGeoData = await axios.get( 23 const metaGeoData = await axios.get(
17 `http://api.openweathermap.org/geo/1.0/direct?q=${city},${countryCode}&limit=${limit}&appid=${process.env.OPENWEATHER_API_KEY}` 24 `http://api.openweathermap.org/geo/1.0/direct?q=${city},${countryCode}&limit=${limit}&appid=${process.env.OPENWEATHER_API_KEY}`
...@@ -40,10 +47,16 @@ const getWeather = asyncHandler(async (req, res) => { ...@@ -40,10 +47,16 @@ const getWeather = asyncHandler(async (req, res) => {
40 // @route GET /api/weather/forecast 47 // @route GET /api/weather/forecast
41 // @access Public 48 // @access Public
42 const getForecast = asyncHandler(async (req, res) => { 49 const getForecast = asyncHandler(async (req, res) => {
43 - const countryCode = "KR"; 50 + const token = req.body.token;
44 - const city = "seoul"; 51 + const decoded = jwt.decode(token, { complete: true });
45 - const limit = 5; 52 + const user = await User.findById(decoded.payload.id)
53 + .select("-password")
54 + .select("-email");
55 +
56 + const countryCode = user.country;
57 + const city = user.city;
46 58
59 + const limit = 5;
47 try { 60 try {
48 const metaGeoData = await axios.get( 61 const metaGeoData = await axios.get(
49 `http://api.openweathermap.org/geo/1.0/direct?q=${city},${countryCode}&limit=${limit}&appid=${process.env.OPENWEATHER_API_KEY}` 62 `http://api.openweathermap.org/geo/1.0/direct?q=${city},${countryCode}&limit=${limit}&appid=${process.env.OPENWEATHER_API_KEY}`
...@@ -72,10 +85,16 @@ const getForecast = asyncHandler(async (req, res) => { ...@@ -72,10 +85,16 @@ const getForecast = asyncHandler(async (req, res) => {
72 // @route GET /api/weather/airpollution 85 // @route GET /api/weather/airpollution
73 // @access Public 86 // @access Public
74 const getAirPollution = asyncHandler(async (req, res) => { 87 const getAirPollution = asyncHandler(async (req, res) => {
75 - const countryCode = "US"; 88 + const token = req.body.token;
76 - const city = "san francisco"; 89 + const decoded = jwt.decode(token, { complete: true });
77 - const limit = 5; 90 + const user = await User.findById(decoded.payload.id)
91 + .select("-password")
92 + .select("-email");
78 93
94 + const countryCode = user.country;
95 + const city = user.city;
96 +
97 + const limit = 5;
79 try { 98 try {
80 const metaGeoData = await axios.get( 99 const metaGeoData = await axios.get(
81 `http://api.openweathermap.org/geo/1.0/direct?q=${city},${countryCode}&limit=${limit}&appid=${process.env.OPENWEATHER_API_KEY}` 100 `http://api.openweathermap.org/geo/1.0/direct?q=${city},${countryCode}&limit=${limit}&appid=${process.env.OPENWEATHER_API_KEY}`
......